How to track down a "double free or corruption" error

neuromancer picture neuromancer · May 25, 2010 · Viewed 247.8k times · Source

When I run my (C++) program it crashes with this error.

* glibc detected * ./load: double free or corruption (!prev): 0x0000000000c6ed50 ***

How can I track down the error?

I tried using print (std::cout) statements, without success. Could gdb make this easier?

Answer

Hasturkun picture Hasturkun · May 25, 2010

If you're using glibc, you can set the MALLOC_CHECK_ environment variable to 2, this will cause glibc to use an error tolerant version of malloc, which will cause your program to abort at the point where the double free is done.

You can set this from gdb by using the set environment MALLOC_CHECK_ 2 command before running your program; the program should abort, with the free() call visible in the backtrace.

see the man page for malloc() for more information