nixers
[C/C++] Valgrind to find memory leaks - Printable Version
+- nixers (https://nixers.net)
+-- Forum: Development & Graphics (https://nixers.net/Forum-Development-Graphics)
+--- Forum: Programming On Unix (https://nixers.net/Forum-Programming-On-Unix)
+--- Thread: [C/C++] Valgrind to find memory leaks (/Thread-C-C-Valgrind-to-find-memory-leaks)


[C/C++] Valgrind to find memory leaks - venam - 24-02-2013

Hello *nixers,
valgrind, from the man page, is a suite of tools for debugging and profiling programs.
I'll introduce you to the memory leak finder feature of valgrind.
(note: valgrind only works on posix compliant OS)

First of all install valgrind http://valgrind.org/downloads.html
It's the default way of installing any software.

cd to the dir of the program you want to test and run the following:
Code:
valgrind --tool=memcheck --leak-check=yes ./the_so_called_program

It will output some infos while the program is running.
Once the program has finish its procedure you should take a look at those outputs (normally at the end) and if you see something that ressembles this it means you have memory leaks:
Code:
==4156== 8 bytes in 2 blocks are definitely lost in loss record 1 of 4

valgrind will also output infos about invalid pointers, not initialized variables, double free, etc...

Sometimes you might want to have more verbose outputs and running this will be more convenient:
Code:
valgrind --tool=memcheck --show-reachable=yes --log-file="logs" --leak-check=yes -v ./the_so_called_program
-v makes it more verbose and "logs" is the logs output that can be analyzed later.

A little end note:
when compiling with gcc you should not use stripping flags and should produce debugging symbols so valgrind can help you detect the lines of codes where there are leaks.


You can find more infos about valgrind on the man 1 valgrind.
(PS: you can use gdb later to debug the program if you found where the leak was but are not really sure)


RE: valgrind to find memory leaks - Mafia - 24-02-2013

I actually never new this, I will be trying this soon. Thanks :)