user862833
user862833

Reputation: 309

how and when does valgrind detect memory leaks

I have a memory leak in my code.The heap goes on increasing with trigger of a flow. In my code I have a while(1) loop which basically waits on a queue. when that flow is triggered from webservice an incoming msg is pushed into the queue, and a method in c++ is invoked which takes the message and implements the flow which is in the form of a graph.

For every trigger i find 604kb of memory getting increased in the heap.I need to resolve this.

I stopped the service [the binary running in prod].

I started the service with valgrind [gave the binary as input to valgrind]

valgrind  --log-file=/home/valgrind-output.txt --leak-check=full <service binary>

But while the binary is running it does not show any leak, only when i stop the service i see some leaks.But those are one time leaks and will be cleared once we restart the service.[binary].

I intentionally added an allocation at the start of the process when each flow is triggered.

 int *p = new int[10];

and added a log also.I see the log printing every time i trigger a flow.But I donot see valgrind giving any leak in the output file.

How can i check the leaks between each flow call? Does valgrind shoes the leaks only once the end of main is called? How to check dynamically what leaks my code is causing in this case. If there is any other detector which i need to install? Please suggest.It would be of great help.Thanks.

Upvotes: 4

Views: 5345

Answers (3)

Nick
Nick

Reputation: 1

This is a very hacky solution, but I learned that if you use fork() and exit it immediately, Valgrind will do a full stack trace and the original program still runs.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126358

valgrind only checks for memory leaks when your program exits. At that point it traces all memory reachable from the stack and global variables, and anything unreachable is considered to be a leak.

If you want to find leaks in a long-running program, you need to insert a call to exit (aborting the program) that will occur sometime after its been running for awhile, and it will show you what has leaked at that point.

Upvotes: 2

Adnan Akbar
Adnan Akbar

Reputation: 718

Use -massif it gives you the detailed output where the memory is being allocated with all the call stack.

Upvotes: -2

Related Questions