如何在列表内的句柄上使用 free?-> C->窗口API

发布于 2024-10-08 23:21:56 字数 2046 浏览 11 评论 0原文

我有一个 C 语言列表,如下所示:

typedef struct _node
{
   int number;
   DWORD threadID;
   HANDLE threadH;
   struct *_node next;
} *node;

您也有这样的列表:

node new_node = malloc(sizeof(node));

正如您可能已经猜到的那样,该列表将存储线程的信息,包括它们的处理程序和 Id。当我尝试执行此操作时,我仍然遇到麻烦:

free(new_node);

每次我尝试执行此操作时,我都会遇到意外错误,VS 说存在数据损坏。我已经尽可能地确定了,当我尝试使用 free 句柄时,我发现问题存在。 我在 MSDN 上搜索了如何执行此操作,但我唯一能找到的是关闭线程的函数(这不是这里的目的,因为我希望线程运行,只需从列表中删除它的记录)。

问题是:我应该如何从内存中释放句柄? (考虑到这只是句柄值的副本,活动句柄不会被删除)。

编辑:这是从列表中插入节点的函数:

int insereVisitanteLista(node* lista, DWORD threadID, HANDLE threadH, int num_visitante)
{
    node visitanteAnterior;
    node novoVisitante = (node)malloc(sizeof(node));

    if(novoVisitante == NULL)
        return 0;

    novoVisitante->threadID = threadID;
    novoVisitante->threadH = threadH;
    novoVisitante->number = num_visitante;
    novoVisitante->next = NULL;

    if(*lista == NULL)
    {
        *lista = novoVisitante;
        return 1;
    }

    visitanteAnterior = *lista;

    while(visitanteAnterior->next != NULL)
        visitanteAnterior = visitanteAnterior->next;

    visitanteAnterior->next =novoVisitante;
    return 1;
}

这​​是删除节点的函数:

int removeVisitanteLista(node * lista, DWORD threadID)
{
    node visitanteAnterior = NULL, visitanteActual;

    if(*lista == NULL)
        return 0;

    visitanteActual = *lista;

    if((*lista)->threadID == threadID)
    {
        *lista = visitanteActual->next;
        visitanteActual->next = NULL;
        free(visitanteActual);

        return 1;
    }

    while(visitanteActual != NULL && visitanteActual->threadID != threadID)
    {
        visitanteAnterior = visitanteActual;
        visitanteActual = visitanteActual->next;
    }

    if (visitanteActual == NULL)
        return 0;

    visitanteAnterior->next = visitanteActual->next;
    free(visitanteActual);

    return 1;
}

I have a list in C that is something like this:

typedef struct _node
{
   int number;
   DWORD threadID;
   HANDLE threadH;
   struct *_node next;
} *node;

And you have somthing like this:

node new_node = malloc(sizeof(node));

As you may have guessed out, this list will store information for threads, including their handlers and Id's. Still I am having trouble when I try to do this:

free(new_node);

Everytime I try to do this I encounter an unexpected error, VS saying that there was a data corruption. I've pinned down as much as possible and I found that the problem resides when I try to use free the handle.
I've searched on MSDN how to do this but the only thing I can find is the function that closes the thread (which is not intended here, since I want the thread to run, just deleting it's record from the list).

The question is: how I am supposed to free an handle from the memory? (Considering that this is only a copy of the value of the handle, the active handle is not being deleted).

EDIT: This is the function to insert nodes from the list:

int insereVisitanteLista(node* lista, DWORD threadID, HANDLE threadH, int num_visitante)
{
    node visitanteAnterior;
    node novoVisitante = (node)malloc(sizeof(node));

    if(novoVisitante == NULL)
        return 0;

    novoVisitante->threadID = threadID;
    novoVisitante->threadH = threadH;
    novoVisitante->number = num_visitante;
    novoVisitante->next = NULL;

    if(*lista == NULL)
    {
        *lista = novoVisitante;
        return 1;
    }

    visitanteAnterior = *lista;

    while(visitanteAnterior->next != NULL)
        visitanteAnterior = visitanteAnterior->next;

    visitanteAnterior->next =novoVisitante;
    return 1;
}

And this is the function to delete nodes:

int removeVisitanteLista(node * lista, DWORD threadID)
{
    node visitanteAnterior = NULL, visitanteActual;

    if(*lista == NULL)
        return 0;

    visitanteActual = *lista;

    if((*lista)->threadID == threadID)
    {
        *lista = visitanteActual->next;
        visitanteActual->next = NULL;
        free(visitanteActual);

        return 1;
    }

    while(visitanteActual != NULL && visitanteActual->threadID != threadID)
    {
        visitanteAnterior = visitanteActual;
        visitanteActual = visitanteActual->next;
    }

    if (visitanteActual == NULL)
        return 0;

    visitanteAnterior->next = visitanteActual->next;
    free(visitanteActual);

    return 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-10-15 23:21:56

您尝试释放的节点到底是什么?这是一个指向 struct _node 的指针吗?如果是,您之前分配过吗?如果不是,则不需要free,否则您必须检查节点是否不NULL,并确保您不会多次free它。如果没有重现问题的最小工作示例,很难猜测您在做什么以及错误在哪里。我唯一可以建议的是阅读有关 C 语言内存管理的内容。此资源可能会有所帮助。

更新:

代码中的节点是指向 _node 的指针。所以 sizeof (node) 是指针的大小,它是 4 或 8 字节(取决于体系结构)。例如,您分配了 8 个字节,但假设您有一个指向更大的结构的指针。结果,你破坏了内存,并且程序的行为变得不确定。因此,将节点 novoVisitante = (node)malloc(sizeof(node)) 更改为节点 novoVisitante = (node)malloc(sizeof(_node)) 应该可以解决问题。

What exactly is a node that you are trying to free? Is this a pointer to a struct _node? If yes, have you allocated it previously? If no, free is not needed, otherwise you have to check if node is not NULL and make sure you do not free it multiple times. It is hard to guess what you are doing and where is an error without a minimal working example reproducing the problem. The only thing I can suggest is to read about memory management in C. This resource might help.

UPDATE:

node in your code is a pointer to _node. So sizeof (node) is a size of a pointer, which is either 4 or 8 bytes (depending on architecture). So you allocate 8 bytes, for example, but assume you have a pointer to the structure which is much larger. As a result, you corrupt memory, and behavior of the program becomes undefined. So changing node novoVisitante = (node)malloc(sizeof(node)) to node novoVisitante = (node)malloc(sizeof(_node)) should fix the problem.

千鲤 2024-10-15 23:21:56

您没有向我们展示您调用 free() 的上下文,因此我需要进行一些推测,但我首先担心的是您没有提到在删除节点之前从列表中删除该节点。

首先通过修改前一个(或头)节点的下一个字段来取消链接节点。如果仍然出现错误,则说明您通过写入已分配的内存结构之一的末尾或类似的内容以某种方式损坏了内存。

另外,我假设节点是一个指针。您确实没有提供太多有关您正在做的事情的信息。

You haven't shown us the context of your call to free() so I need to speculate a little but my first concern is that you didn't mention removing the node from the list before deleting it.

Start by unlinking the node by modifying the next field of the previous (or head) node. If you still get the error, then you have corrupted memory somehow by writing past the end of one of your allocated memory structures or something similar.

Also, I assume node is a pointer. You really haven't provided much information about what you're doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文