Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug heap/STL debugging equivalent for GCC?

Tags:

c++

gcc

debugging

I plan on using GCC more (Linux and Windows) and I was wondering if there's an equivalent of the MSVC debug heap and the STL checks available for the GCC CRT and STL.

I already know about tools such as Valgrind, but I'm looking for something built in the libraries.

like image 403
rpg Avatar asked Jul 24 '09 12:07

rpg


1 Answers

I'm not too familiar with the debug heap and STL checks, but when I have memory problems in GCC on linux I use an environment variable called MALLOC_CHECK_ (from malloc(3)):

Recent versions of Linux libc (later than 5.4.23) and GNU libc (2.x) include a malloc implementation which is tunable via environment variables. When MALLOC_CHECK_ is set, a special (less efficient) implementation is used which is designed to be tolerant against simple errors, such as double calls of free() with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be protected against, however, and memory leaks can result. If MALLOC_CHECK_ is set to 0, any detected heap corruption is silently ignored; if set to 1, a diagnostic is printed on stderr; if set to 2, abort() is called immediately. This can be useful because otherwise a crash may happen much later, and the true cause for the problem is then very hard to track down.

There is also Electric Fence which can help catch buffer overruns aborting as soon as the overrun / underrun happens. See libefence(3) for more information.

like image 181
Peter Kovacs Avatar answered Oct 02 '22 16:10

Peter Kovacs