避免与内存分配相关的错误
我有一个复杂的 C 代码,在执行它时,我偶然发现了以下错误:
- glibc: 损坏的双链表
- glibc: malloc() 内存损坏
- 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:
- glibc: corrupted double-linked list
- glibc: malloc() memory corruption
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在黑暗中进行尝试,猜测您在使用
malloc( 分配之前尝试
。free()
ptr
指针 )如果它已初始化为 NULL,则大多数 free() 实现不会执行任何操作。从
free()
手册页:如果它尚未设置为 NULL,则您正在尝试释放随机指针或已释放的内容。
也就是说,Valgrind 是在 POSIX 系统上正确检测此类错误的最佳工具。
编辑:
需要理解的是,C 不是 Java,而且它没有 VM 的优点。所有内容都存在于同一地址空间内,具有最小的保护 - 其中包括内存分配器的结构。一旦发生与内存相关的错误,就无法预测它会如何被发现。
在其他两个错误中,乍一看我猜想 (3) 再次与释放尚未分配的地址有关。然而,没有办法确定这确实是问题所在。一旦进程的内存被损坏,您就无法信任它告诉您的有关其自身的任何信息 - 这就是本例中发生的情况。
只需使用适当的调试工具,如 GDB 或 Valgrind,就可以避免你(和我们)盲目猜测的痛苦......
I'll take a shot in the dark and guess that you attempt to
free()
theptr
pointer before allocating withmalloc()
.If it has been initialized to
NULL
, mostfree()
implementations do nothing. From thefree()
manual page: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...