避免与内存分配相关的错误

发布于 2024-11-18 04:20:07 字数 1130 浏览 2 评论 0原文

我有一个复杂的 C 代码,在执行它时,我偶然发现了以下错误:

  1. glibc: 损坏的双链表
  2. glibc: malloc() 内存损坏
  3. munmap_chunk() 无效指针

我意识到 1) 与释放已释放的内存相关。我仍在试图找出2)和3)的原因。

好吧,事情是然后我做了一些搜索并得到了一般意见,我必须使用“valgrind”进行调试以检测与内存损坏相关的问题。

好吧,回到正题,当我搜索这个论坛时,我刚刚挖出了一些发布在以下位置的代码: 从错误返回后释放内存的最佳方法是什么?

这段代码解决了我的问题:

int func(void **mem1, void **mem2)
{
    *mem1 = NULL;
    *mem2 = NULL;

    *mem1 = malloc(SIZE);
    if(!*mem1)
        goto err;

    *mem2 = malloc(SIZE);
    if(!*mem2)
        goto err;

    return 0;
err:
    if(*mem1)
        free(*mem1);
    if(*mem2)
        free(*mem2);

    *mem1 = *mem2 = NULL;

    return 1;
}

那么真正解决了什么我的问题是:

例如:

char *ptr = NULL;

ptr = (char *)malloc(SIZE);

assign and use ptr

free(ptr);

怎么样char *ptr = NULL 有帮助吗???事实上,当我一开始分配给NULL时,我什至没有使用free(ptr)。它仍然像一个魅力一样工作(我尝试执行几次)

当我在开始时删除 NULL 赋值时,我收到错误 1) :( :(

我要安装 Valgrind 但在此之前我想对此有一些见解。

谢谢

I have a complex C code with me and while executing it, I chanced upon the following errors:

  1. glibc: corrupted double-linked list
  2. glibc: malloc() memory corruption
  3. munmap_chunk() invalid pointer

I realized 1) is associated with freeing already freed memory. I am still trying to figure out the reasons for 2) and 3).

Well, the thing is then I did some searches and got the general opinion that I must debug with "valgrind" to detect memory corruption related problems.

Ok, coming back to the point,when I searched this forum, I have just dug up some code posted at: What is the best way to free memory after returning from an error?

And this piece of code had solved my problems:

int func(void **mem1, void **mem2)
{
    *mem1 = NULL;
    *mem2 = NULL;

    *mem1 = malloc(SIZE);
    if(!*mem1)
        goto err;

    *mem2 = malloc(SIZE);
    if(!*mem2)
        goto err;

    return 0;
err:
    if(*mem1)
        free(*mem1);
    if(*mem2)
        free(*mem2);

    *mem1 = *mem2 = NULL;

    return 1;
}

Well what really solved my issue is the line:

eg:

char *ptr = NULL;

ptr = (char *)malloc(SIZE);

assign and use ptr

free(ptr);

How is char *ptr = NULL helping???? Infact when I assigned to NULL in the beginning, I didn't even use free(ptr). It still worked liked a charm(I tried executing several times)

When I remove the NULL assignment in the beginning I get error 1) :( :(

I am going to install Valgrind but before that I would like some insights on this.

Thanks

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

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

发布评论

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

评论(1

娇女薄笑 2024-11-25 04:20:07

我会在黑暗中进行尝试,猜测您在使用 malloc( 分配之前尝试free() ptr 指针

如果它已初始化为 NULL,则大多数 free() 实现不会执行任何操作。从 free() 手册页:

free() 释放指向的内存空间
到 ptr ,这一定是
由先前的调用返回
malloc()、calloc() 或 realloc()。
否则,或者如果 free(ptr) 已经
之前被调用过,未定义
行为发生。 如果 ptr 为 NULL,则不
执行操作。

如果它尚未设置为 NULL,则您正在尝试释放随机指针或已释放的内容。

也就是说,Valgrind 是在 POSIX 系统上正确检测此类错误的最佳工具。

编辑:

需要理解的是,C 不是 Java,而且它没有 VM 的优点。所有内容都存在于同一地址空间内,具有最小的保护 - 其中包括内存分配器的结构。一旦发生与内存相关的错误,就无法预测它会如何被发现。

在其他两个错误中,乍一看我猜想 (3) 再次与释放尚未分配的地址有关。然而,没有办法确定这确实是问题所在。一旦进程的内存被损坏,您就无法信任它告诉您的有关其自身的任何信息 - 这就是本例中发生的情况。

只需使用适当的调试工具,如 GDB 或 Valgrind,就可以避免你(和我们)盲目猜测的痛苦......

I'll take a shot in the dark and guess that you attempt to free() the ptr pointer before allocating with malloc().

If it has been initialized to NULL, most free() implementations do nothing. From the free() manual page:

free() frees the memory space pointed
to by ptr, which must have been
returned by a previous call to
malloc(), calloc() or realloc().
Otherwise, or if free(ptr) has already
been called before, undefined
behaviour occurs. If ptr is NULL, no
operation is performed.

If it has not been set to NULL, you are trying to free either a random pointer, or something that has been already been freed.

That said, Valgrind is the best tool to properly detect such errors on POSIX systems.

EDIT:

What needs to be understood it that C is not Java and it does not have the luxuries of a VM. Everything exists within the same address space, with minimal protections - and that includes the structures of the memory allocator. Once a memory-related error occurs, there is no way to predict how it will make itself known.

Of the other two errors, I'd guess at first glance that (3) is once again related to freeing an address that has not been allocated. There is no way, however, to be sure that this is actually the issue. Once the memory of a process is corrupted, you cannot trust anything it tells you about itself - which is what's happening in this case.

Just use proper debugging tools like GDB or Valgrind and save yourself (and us) the pain of guessing blindly...

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