Debug your “double free or corruption” errors
I’ve seen a couple of instances now where people are using delete or free for IplImages. This will, as far as I know, always result in a memory error at runtime, which on Ubuntu, looks something like this:
*** glibc detected *** ./analyser: double free or corruption (out): 0x09b0ef50 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0x2867ff1]
/lib/tls/i686/cmov/libc.so.6[0x28696f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x286c7cd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xeb76f1]
./analyser(_ZN12bct_analysis12processImageEPK9_IplImageiiii+0x80)[0x804d12b]
./analyser(main+0x6f2)[0x804abb6]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x2813b56]
./analyser[0x804a431]
======= Memory map: ========
00110000-001a3000 r-xp 00000000 08:01 5861 /usr/lib/libgio-2.0.so.0.2200.3
001a3000-001a4000 r--p 00092000 08:01 5861 /usr/lib/libgio-2.0.so.0.2200.3
001a4000-001a5000 rw-p 00093000 08:01 5861 /usr/lib/libgio-2.0.so.0.2200.3
So, we can see from the error the cause is “double free or corruption” in libc. This error isn’t unique to OpenCV and is discussed in a lot of places. For some reason a frequently recommended fix for this seems to be to disable memory error checks by setting the environment variable MALLOC_CHECK_.
Errors and warning messages are your friends. Very rarely should you suppress such messages and hope for the best. The only acceptable time to ignore them is when you’ve made promises about being finished, you’re due to deliver and you’re pretty sure you can get a bug fix out before someone notices. Or if it’s nearly lunch time. Of course, desktop users who just want to get on with using some program, you’ve done nothing wrong – feel free to blithely carry on!
The “error” is caused by glibc’s mcheck. The best solution is to debug your code. IDEs like Netbeans and (*sigh*) eclipse make this pretty trivial. If you’re too cool for X gdb is easy enough to get to grips with, and is what underlies the graphical environments anyway. Personally, I like Valgrind for memory and profiling issues; though in many cases simple code stepping will hopefully make multiple uses of free apparent.
mcheck is purely diagnostic, if a little invasive, more iformation can be found in the glibc documentation and man 3 of malloc:
If MALLOC_CHECK_ is set to 0, any detected heap corruption is silently ignored; if set to 1, a diagnostic message is printed on stderr; if set to 2, abort(3) is called immediately; if set to 3, a diagnostic message is printed on stderr and the program is aborted.
Double freeing can have unpredictable behaviour. Admittedly in many cases it will can go unnoticed, but why risk it? Buffer overflows and corruption of memory structures can occur and cause drastic failure.
In the case of delete/free with OpenCV the solution is to use the provided cvReleaseImage() for C (or Mat::release() in C++). OpenCV has internal allocate and free functions (which are by default malloc and free, wrapped in some magic); which is always best, particularly when dealing with complex types where pointer-to-pointer members may occur.










