Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valgrind and Memory Leaks

I'm doing a little bit of memory profiling to my software and after running standard memory leak check with valgrind's following command

valgrind --tool=memcheck --leak-check=full ./path_to_program

I got following summary:

==12550== LEAK SUMMARY:

==12550== definitely lost: 597,170 bytes in 7 blocks

==12550== indirectly lost: 120 bytes in 10 blocks

==12550== possibly lost: 770,281 bytes in 1,455 blocks

==12550== still reachable: 181,189 bytes in 2,319 blocks

==12550== suppressed: 0 bytes in 0 blocks

==12550== Reachable blocks (those to which a pointer was found) are not shown.

==12550== To see them, rerun with: --leak-check=full --show-reachable=yes

==12550==

==12550== For counts of detected and suppressed errors, rerun with: -v

==12550== ERROR SUMMARY: 325 errors from 325 contexts (suppressed: 176 from 11)

It doesn't look quite good to me, so my question is

Why isn't my program exploding if it has all these leaks?

And also what is the difference between:

  • definitely lost
  • indirectly lost
  • possibly lost
  • still reachable

and how can I try to fix them?

like image 407
Matteo Avatar asked Mar 31 '12 13:03

Matteo


1 Answers

I suggest visiting the Valgrind FAQ:

With Memcheck's memory leak detector, what's the difference between "definitely lost", "indirectly lost", "possibly lost", "still reachable", and "suppressed"?

The details are in the Memcheck section of the user manual.

In short:

  • "definitely lost" means your program is leaking memory -- fix those leaks!

  • "indirectly lost" means your program is leaking memory in a pointer-based structure. (E.g. if the root node of a binary tree is "definitely lost", all the children will be "indirectly lost".) If you fix the "definitely lost" leaks, the "indirectly lost" leaks should go away.

  • "possibly lost" means your program is leaking memory, unless you're doing unusual things with pointers that could cause them to point into the middle of an allocated block; see the user manual for some possible causes. Use --show-possibly-lost=no if you don't want to see these reports.

  • "still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.

  • "suppressed" means that a leak error has been suppressed. There are some suppressions in the default suppression files. You can ignore suppressed errors.

like image 126
Oliver Charlesworth Avatar answered Nov 10 '22 04:11

Oliver Charlesworth