Valgrind - memory leak and memory corruption detector

Valgrind tool is the memory mis management detector. it can detect

1) memory leaks
2) un initialised memory
3)Mismatched use of malloc/new/new [] vs free/delete/delete []
4) Overlapping src and dst pointers in memcpy() and related functions
5)reading or writing from/to pointers after deleting or freeing that pointer etc.

valgrind is wrapper of many tools. among them lets have a look at memcheck tool.

how to use valgrind ?

$valgrind --tool= tool_name --leakcheck= yes executable(ex: a.out)

by default the tool_name will be 'memcheck'


what is a memory leak?
when we allocate memory dynamically (at run time) , and if we dont free it after use, until the termination of program that memory cannot be re-gained. this is called memoy leak.

see the following program prog1.cpp which creates two pointers p and s at runtime on heap. we freed the memory pointed by p, but we didnt freed the memory pointed by s. so the memory pointed by s will not be re used until termination of program.

lets use the valgrind tool to detect the memory leak.



#include"iostream"
using namespace std;
int main()
{
char *p= new char[10];
delete []p;
char *s=new char[20];
return 1;
}

$g++ -g prog.cpp

$valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out

always the first column will be pid of your process.

==13993== Memcheck, a memory error detector.
==13993== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==13993== Using LibVEX rev 1732, a library for dynamic binary translation
.==13993== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==13993== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==13993== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==13993== For more details, rerun with: -v
==13993====
13993====
13993== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==13993== malloc/free: in use at exit: 20 bytes in 1 blocks
.==13993== malloc/free: 2 allocs, 1 frees, 30 bytes allocated.
==13993== For counts of detected errors, rerun with: -v
==13993== searching for pointers to 1 not-freed blocks.
==13993== checked 104,344 bytes.
==13993
====13993== 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
==13993== at 0x401B085: operator new[](unsigned) (vg_replace_malloc.c:195) ==13993== by 0x8048595: main (in /home/edarav/plusplus/a.out)
==13993
====13993== LEAK SUMMARY:
==13993== definitely lost: 20 bytes in 1 blocks.
==13993== possibly lost: 0 bytes in 0 blocks.
==13993== still reachable: 0 bytes in 0 blocks.
==13993== suppressed: 0 bytes in 0 blocks.

the bold lines in LEAK SUMMARY will give you how much memory was lost . here we lost 20 bytes.

In our c++ program above we allocated 20 bytes for char *s and we didnt free it. so the report shows those 20 bytes. like wise valgrind memcheck tool can also detect the un initialsed memory.

Common valgrind options

Option : --num-callers=number
Purpose : Determines the number of function calls (i.e. depth of stacktrace) to display as part of showing where an error occurs within a program. The default is a measly 4.

Option : --leak-check=yes
Purpose : Enabling leak checking has valgrind search for memory leaks (i.e. allocated memory that has not been released) when the program finished.

Option : --leak-resolution=high
Purpose : An option that should be used when doing leak checking since all other options result in confusing reports.

Option : --show-reachable=yes
Purpose : An option that makes leak checking more helpful by requesting that valgrind report whether pointers to unreleased blocks are still held by the program.

Option : -v
Purpose : Run in more verbose mode.

Option : -fno-inline
Purpose : An option for C++ programs which makes it easier to see the function-call chain.

Option : --gen-suppressions=yes
Purpose : A simple way to generate a suppressions file in order to facilitate ignoring certain errors in future runs of the same code.

Option : --skin=addrcheck
Purpose : (Note that the name of this option has become --tool and has become mandatory for the development release). This selects the specific tool of valgrind that will run. Memcheck (the only tool covered here) is the default.

Option : --logfile=file-basename
Purpose : Record all errors and warnings to file-basename.pidpid

No comments: