3 ms·
Valgrind can also dump leak information while the program is running. For that, you have to use the Valgrind API. There is a request for it. #include <valgr
by kazinator 11y ago
Valgrind can also dump leak information while the program is running. For that, you have to use the Valgrind API. There is a request for it.
#include <valgrind/memcheck.h>
...
// in the code:
VALGRIND_DO_LEAK_CHECK;
I like to put
#ifdef CONFIG_VALGRIND_SUPPORT
around this stuff so it can be compiled out. The Valgrind API call macros expand to some machine code instruction patterns which execute harmlessly on a regular CPU, and have no effect, but are recognized as triggers by the Valgrind execution core. These instructions add bloat and burn cycles.
Valgrind has the advantage that it has a real leak checker: i.e. reports of still-allocated memory that is definitely unreachable by any pointer. It can also report blocks which are reachable only by pointers into their interior, not to their base address.
Having the stack trace for each allocation is also nice. If something is leaked, it can sometimes be useful to know where it was allocated (by what chain of calls). Of course you might know that every dynamic struct foo came from foo_alloc. But who called foo_alloc for a given leaked struct foo? And who called that caller?