C - 释放分配在另一个文件中的堆内存
如果您有一个项目使用 makefile 来编译多个文件和头文件,这会使堆变得复杂吗?
具体来说: 我有一个 main.c
文件,其中包含一个标头,例如 test.h
。在链接到 test.h
的 test.c
中,内存是使用 malloc 显式分配的。 main.c
调用test.c
中的函数。由于某种原因,当我尝试释放 test.c 中的函数内部的内存时,我总是收到错误:
main(65245) malloc: *** error for object 0x106d012f8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
...即使我从来没有释放过任何内存,也会发生此错误全部在整个 makefile 堆栈中。显然包含了 stdlib.h
。可能发生什么事? main.c
和 test.c
是否有单独的堆,每当调用后者并返回结果时,分配的堆内存已经被释放?我真的很困惑。我可以在 main.c
中分配和释放内存,没有任何问题。它们具有相同的包含内容。
If you have a project that utilizes a makefile which compiles multiple files and headers, does this complicate the heap?
Specifically:
I have a main.c
file which includes a header, say test.h
. In test.c
which is linked to test.h
memory is allocated explicitly with malloc. main.c
calls the functions in test.c
. For some reason, when I try to to free memory inside of the functions in test.c
I always get an error:
main(65245) malloc: *** error for object 0x106d012f8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
...this error occurs even though I never, not even once free any memory at all in the entire makefile stack. Obviously stdlib.h
is included. What could be going on? Are there separate heaps for main.c
and test.c
and whenever the latter is called and the result is returned, the heap memory allocated is already freed? I'm really stumped. I can allocate and free memory in main.c
without any issue. They have the same includes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
运行时不存在“不同文件”之类的东西。所有文件在链接时都集成为一个大的二进制代码。因此,显然只有一个堆。
你的问题一定是别的问题,因为你从未释放过内存,也许你正在尝试释放静态分配的内存或类似的东西
,请注意,有一个约定通常可以很好地防止内存泄漏,其中表示:分配内存的程序部分也负责释放内存。它与您的问题没有直接关系,但对于将来尝试这样做会有所帮助,以防止内存泄漏。
there is no such thing as 'different files' at run-time. all files are integrated into one big binary code at linkage. So, therefore, there is obviously only one heap.
your problem must be something else, since you never freed a memory, maybe you are trying to free static allocated memory or something like that
also, note that there is a convention which is usually pretty good to prevent memory leaks, which says: the part of the program that allocated the memory, is also responsible to free it. It is not directly connected to your question, but it will be helpful for future to try and do it, in order to prevent memory leaks.
您是否在每个文件中释放相同的地址/指针?是的,除非您专门尝试获得多个堆,否则您将获得 1 个堆。我的猜测是你没有释放同一个指针 - 也许是某种寻址/双指针错误。您最好为我们发布一些源代码,以确保......
Are you freeing the same addres/pointer in each file? Yes, you get 1 heap unless you specifically try and get multiple heaps. My guess is you are not freeing the same pointer - perhaps a addressing/double pointer error of some sort. You do best to post some source code for us to be sure...
所有 malloc() 调用都从同一堆分配,无论您从哪个文件调用它们。您正在释放一个并非来自任何 malloc() 调用的指针。
仔细检查您的代码,将分配和释放的指针值打印到调试日志。
All malloc() calls allocate from the same heap, no matter what file you call them from. You are freeing a pointer that did not come from any malloc() call.
Review your code carefully, print allocated and freed pointer values to debug log.