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?
Description. Double free errors occur when free() is called more than once with the same memory address as an argument. Calling free() twice on the same value can lead to memory leak.
The error of double free or corruption in C++ means that our program somehow invokes the free() C++ object with an illegal pointer variable. When we use smart pointers such as shared_ptr, we must check because if we call the function get(), we are directly using the raw pointer.
You can use gdb, but I would first try Valgrind. See the quick start guide. Briefly, Valgrind instruments your program so it can detect several kinds of errors in using dynamically allocated memory, such as double frees and writes past the end of allocated blocks of memory (which can corrupt the heap).
Double Free A simple technique to avoid this type of vulnerability is to always assign NULL to a pointer after it has been freed. Subsequent attempts to free a null pointer will be ignored by most heap managers.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With