How to track down a "double free or corruption" error
Solution 1:
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
Solution 2:
There are at least two possible situations:
- you are deleting the same entity twice
- you are deleting something that wasn't allocated
For the first one I strongly suggest NULL-ing all deleted pointers.
You have three options:
- overload new and delete and track the allocations
- yes, use gdb -- then you'll get a backtrace from your crash, and that'll probably be very helpful
- as suggested -- use Valgrind -- it isn't easy to get into, but it will save you time thousandfold in the future...