Valgrind: Memory leak or no?

2019-03-03 00:05发布

问题:

I'm running valgrind on my program and I'm getting the following output (I'm gonna omit the 83 errors above this, let me know if I should include them in the log):

==9723== LEAK SUMMARY:
==9723==    definitely lost: 0 bytes in 0 blocks
==9723==    indirectly lost: 0 bytes in 0 blocks
==9723==      possibly lost: 4,676 bytes in 83 blocks
==9723==    still reachable: 88,524 bytes in 579 blocks
==9723==         suppressed: 0 bytes in 0 blocks
==9723== Reachable blocks (those to which a pointer was found) are not shown.
==9723== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==9723== 
==9723== For counts of detected and suppressed errors, rerun with: -v
==9723== ERROR SUMMARY: 83 errors from 83 contexts (suppressed: 3 from 3)

This is the output I get from valgrind no matter how long I run my program, whether it's 2 seconds or 2 minutes.

Since 'possibly lost' doesn't increase over time, is it safe to assume that I do not have a memory leak?

The errors all seem to come from libglib and revolve around g_malloc0 and g_realloc.

回答1:

Possibly lost errors in valgrind cover a subset of scenarios involving pointer chains. I would definitely chase the cause of this down, until you can confirm it's not an issue (at the very least, your memory footprint shouldn't be growing), since it can indicate other logic problems in your code.

This post has an answer that addresses it in more detail.

For more information, you can also have a look at the relevant section in the valgrind manual.