Debug memory errors with valgrind

For debugging memory errors valgrind is very useful tool.

broken program would show

$ ./program
*** glibc detected *** double free or corruption (out): 0x08138a80 ***
Aborted

valgrind shows where the problem occurs

$ valgrind ./program</div>
<div>==31784== Invalid free() / delete / delete[]
==31784==    at 0x1B90579D: free (vg_replace_malloc.c:152)
==31784==    by 0x1BB2E6CD: xfree (xmalloc.c:200)
==31784==    by 0x1BB25095: _xFreePtr (mem.c:613)

For debugging with valgrind I use following configuration:

~/.valgrindrc file

--tool=memcheck
--memcheck:leak-check=yes
--memcheck:show-reachable=yes
--num-callers=16
--memcheck:db-attach=no

To disable glibc memory errors detection you need to set MALLOC_CHECK_ environment variable to 0 (note the ending underscore).

export MALLOC_CHECK_=0
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>