一个糟糕的免费()?坏删除?坏的malloc?
是的,我认为这是其中之一。
如果错误很容易重现,请在 malloc.c 中放置一个断点,malloc_printerr。当调试器停在那里时,您可能会获得完整的调用堆栈并在您的代码中找到错误的地方。我仍然不知道为什么会这样,进入 __libc_message 后,调用堆栈被破坏。
这就是我发现这种奇怪行为的方式。删除相同缓冲区两次的简单应用程序:
void main()
{
char * buf = new char[4*1024];
delete[] buf;
delete[] buf;
}
在 malloc_printerr 中,调用堆栈如下所示:
#0 malloc_printerr (action=3, str=0x297d0b5c "double free or corruption (top)", ptr=<value optimized out>) at malloc.c:5887
#1 0x29750be8 in __libc_free (mem=0x411008) at malloc.c:3622
#2 0x29612c70 in operator delete (ptr=<value optimized out>) at ../../../../libstdc++-v3/libsupc++/del_op.cc:49
#3 0x29612cc2 in operator delete[] (ptr=<value optimized out>) at ../../../../libstdc++-v3/libsupc++/del_opv.cc:37
#4 0x0040068a in main (argc=1, argv=0x7bb26814) at double_free.cpp:47
输入 __libc_message 后:
#0 __libc_message (do_abort=2, fmt=0x297d09c8 "*** glibc detected *** %s: %s: 0x%s *** ") at ../sysdeps/unix/sysv/linux/libc_fatal.c:50
#1 0x2974f3a8 in malloc_printerr (action=3, str=0x297d0b5c "double free or corruption (top)", ptr=<value optimized out>) at malloc.c:5887
#2 0x297f3700 in _IO_wide_data_2 () from /cygdrive/c/STM/SH4-Linux-gcc/opt/STM/STLinux2.3/devkit/sh4/target/lib/libc.so.6
Backtrace stopped: frame did not save the PC
也许它与属性((noreturn))和编译器优化有关?