Gayan Pathirage
Gayan Pathirage

Reputation: 2089

How to check actual memory leaks during runtime using Valgrind?

In some programs some of the allocated memory is not destroyed at all, but they are required for the entire run time of the program. Hence, generally considered safe.

But there are other objects which are not intended for the entire run time of the program, but not destroyed due to developer misses. These are the actual memory leaks which should be addressed.

When we run the following Valgrind command, it only displays the total leaks after program execution ends. Hence, could someone clarify how to differentiate above two scenarios from Valgrind leak check output.

Command I used to detect memory leaks;

valgrind --log-file=valgrind_output.txt --tool=memcheck --leak-check=yes ./MyTestProgram

Typical output at the end of execution;

==10108== LEAK SUMMARY:
==10108==    definitely lost: 392,323 bytes in 1,164 blocks
==10108==    indirectly lost: 178,120 bytes in 4,283 blocks
==10108==      possibly lost: 170,155,118 bytes in 3,347,087 blocks
==10108==    still reachable: 263,778,326 bytes in 3,935,669 blocks

Is there a feature in Valgrind like Tap in IBM Purify tool, which can detect currently leaked memory during runtime?

Upvotes: 4

Views: 7476

Answers (3)

Martin
Martin

Reputation: 3592

You can make your program deliberately crash during run to make sure you can inspect allocated memory at that point. You can do this by adding a signal handler for some user defined signal like SIGUSR1.

signal(SIGUSR1, myhandler); 

in your handler then you do something like this:

printf("debug exit!\n"); 
int *ptr = 0; 
*ptr = 0xdeadbeef; 

You can then send this signal to your app like this:

kill -s SIGUSR1 `ps -aux| grep myapp | head -n -1 | awk '{print $2}'`

Then you can inspect differences in numbers of allocated objects. If you know that numbers should remain same or if some number keeps growing then you can inspect the place where that happens and see if you have a memory leak there.

Upvotes: 0

phd
phd

Reputation: 3807

You can use 2 different techniques to do leak search during execution.

  1. From the shell command, launch vgdb leak_check There are other optional arguments to leak_check monitor commands, e.g. to find reachable memory, or just the memory that increased or .... See valgrind user manual for details: http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.monitor-commands

  2. Inside your program: You can add in your program client requests to do leak search. E.g. insert "calls" to VALGRIND_DO_LEAK_CHECK or VALGRIND_DO_ADDED_LEAK_CHECK See http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs for more details

Upvotes: 4

BЈовић
BЈовић

Reputation: 64193

Is there a feature in Valgrind like Tap in IBM Purify tool, which can detect currently leaked memory during runtime?

No, there isn't. Valgrind can't know if there is a leak, unless the program finished, because it can't know what will be released when the program ends.

Upvotes: 1

Related Questions