Monday, March 14, 2011

Valgrind usage

Valgrind is a such a great tool to identify memory leaks, invalid frees, usage.. etc.
I would like give brief intro

Install valgrind on Ubuntu
apt-get install valgrind

Example Programme:
-----------------------------
main()
{
int *x;
x=malloc(10);
x=malloc(10);
free(x);
free(x);
}
-----------------------------

Valgrind report:
root@ubuntu:/home/manu/valgrindtest# valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out
==4060== Memcheck, a memory error detector
==4060== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4060== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==4060== Command: ./a.out
==4060==
==4060== Invalid free() / delete / delete[]
==4060== at 0x4C270BD: free (vg_replace_malloc.c:366)
==4060== by 0x40059F: main (in /home/manu/valgrindtest/a.out)
==4060== Address 0x51b0090 is 0 bytes inside a block of size 10 free'd
==4060== at 0x4C270BD: free (vg_replace_malloc.c:366)
==4060== by 0x400593: main (in /home/manu/valgrindtest/a.out)
==4060==
==4060==
==4060== HEAP SUMMARY:
==4060== in use at exit: 10 bytes in 1 blocks
==4060== total heap usage: 2 allocs, 2 frees, 20 bytes allocated
==4060==
==4060== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4060== at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==4060== by 0x400575: main (in /home/manu/valgrindtest/a.out)
==4060==
==4060== LEAK SUMMARY:
==4060== definitely lost: 10 bytes in 1 blocks
==4060== indirectly lost: 0 bytes in 0 blocks
==4060== possibly lost: 0 bytes in 0 blocks
==4060== still reachable: 0 bytes in 0 blocks
==4060== suppressed: 0 bytes in 0 blocks
==4060==
==4060== For counts of detected and suppressed errors, rerun with: -v
==4060== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)


Identified
Invalid frees
Leaks

There is more than this. wait and see. Pin It
Related Posts Plugin for WordPress, Blogger...