OS X 10.5 malloc 中的错误?
我正在用 C 编写一个程序。我有两台主要的开发机器,都是 Mac。一台运行 OS X 10.5,是 32 位计算机,另一台运行 OS X 10.6,是 64 位计算机。该程序在 64 位机器上编译和运行时工作正常。然而,当我在 32 位机器上编译完全相同的程序时,它会运行一段时间,然后在 malloc
内的某个地方崩溃。这是回溯:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xeeb40fe0
0x9036d598 in small_malloc_from_free_list ()
(gdb) bt
#0 0x9036d598 in small_malloc_from_free_list ()
#1 0x90365286 in szone_malloc ()
#2 0x903650b8 in malloc_zone_malloc ()
#3 0x9036504c in malloc ()
#4 0x0000b14c in xmalloc (s=2048) at Common.h:185
...
xmalloc
是我的自定义包装器,如果 malloc
返回 NULL
,它只会调用 exit
,因此它没有运行内存不足。
如果我将相同的代码与 -ltcmalloc
链接,它可以正常工作,因此我强烈怀疑这是 OS X 10.5 默认分配器中的某个错误。可能是我的程序在某处导致了一些内存损坏,并且 tcmalloc 不知何故不会被它绊倒。我尝试通过在不同的程序中执行相同的 malloc
和 free
序列来重现失败,但效果很好。
所以我的问题是:
以前有人见过这个错误吗?或者,
我如何调试这样的东西?例如,OS X 的
malloc
是否有调试版本?
顺便说一句,这些是链接库:
$ otool -L ./interp
./interp:
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.5)
更新:是的,由于写入数组末尾而导致堆损坏,它现在正在工作。我应该在发布问题之前运行 valgrind 。尽管如此,我对如何防止此类损坏的技术(除了 valgrind 之外)很感兴趣,所以谢谢你。
I'm writing a program in C. I have two main development machines, both Macs. One is running OS X 10.5 and is a 32bit machine, the other is running OS X 10.6 and is 64 bits. The program works fine when compiled and run on the 64bit machine. However, when I compile the exact same program on the 32bit machine it runs for a while and then crashes somewhere inside malloc
. Here's the backtrace:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xeeb40fe0
0x9036d598 in small_malloc_from_free_list ()
(gdb) bt
#0 0x9036d598 in small_malloc_from_free_list ()
#1 0x90365286 in szone_malloc ()
#2 0x903650b8 in malloc_zone_malloc ()
#3 0x9036504c in malloc ()
#4 0x0000b14c in xmalloc (s=2048) at Common.h:185
...
xmalloc
is my custom wrapper which just calls exit
if malloc
returns NULL
, so it's not running out of memory.
If I link the same code with -ltcmalloc
it works fine, so I strongly suspect that it's a bug somewhere inside OS X 10.5's default allocator. It may be that my program is causing some memory corruption somewhere and that tcmalloc
somehow doesn't get tripped up by it. I tried to reproduce the failure by doing the same sequence of malloc
s and free
s in a different program but that worked fine.
So my questions are:
Has anyone seen this bug before? Or, alternatively
How can I debug something like this? E.g., is there a debug version of OS X's
malloc
?
BTW, these are the linked libraries:
$ otool -L ./interp
./interp:
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.5)
Update: Yeah, it's heap corruption due to writing past the end off an array, it's working now. I should have run valgrind
before posting the question. I was nevertheless interested in techniques (other than valgrind) how to protect from such kind of corruption, so thanks for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您读过 MacOS X 上
malloc()
的手册页吗?它的部分内容是:调试分配错误
环境
也就是说,我仍然会首先使用 valgrind。
Have you read the manual page for
malloc()
on MacOS X? In part, it says:DEBUGGING ALLOCATION ERRORS
ENVIRONMENT
That said, I'd still use
valgrind
first.吗?是的,这是常见的编程错误,几乎肯定存在于您的代码中。请参阅http://www.efnetcpp.org/wiki/Heap_Corruption
请参阅上述链接的工具部分。
Yes, this is common programming bug and is almost certainly in your code. See http://www.efnetcpp.org/wiki/Heap_Corruption
See the Tools section of the above link.