Skip to content
systemcoding.dev
Debugging

Report every leak category with --show-leak-kinds=all

See still-reachable and indirect leaks in Valgrind 3.23 with --show-leak-kinds=all, which --leak-check=full hides by default.

1 min read
Summarize in AI

Valgrind says "All heap blocks were freed" or reports a few leaks, and you suspect it is leaving some out. It is: with --leak-check=full the default kinds shown are only definite and possible. A block you still hold a pointer to at exit is "still reachable", and it stays hidden until you ask.

c
#include <stdlib.h>

static char *cache;

int main(void) {
    cache = malloc(64);
    return 0;
}
Text
valgrind --leak-check=full --show-leak-kinds=all ./demo
Text
==4242== HEAP SUMMARY:
==4242==     in use at exit: 64 bytes in 1 blocks
==4242==   total heap usage: 1 allocs, 0 frees, 64 bytes allocated
==4242==
==4242== 64 bytes in 1 blocks are still reachable in loss record 1 of 1
==4242==    at 0x484A2F3: malloc (vg_replace_malloc.c:446)
==4242==    by 0x40113A: main (demo.c:6)
==4242==
==4242== LEAK SUMMARY:
==4242==    definitely lost: 0 bytes in 0 blocks
==4242==    still reachable: 64 bytes in 1 blocks

The PID and addresses will differ. A reachable block is not a bug by itself, since a global that lives until exit needs no free, but it is how a cache that grows forever looks, and it is the category a library's internal leaks land in. --show-leak-kinds=definite,indirect,possible,reachable is the long spelling of all. See the Memcheck leak options in the Valgrind 3.23 manual.

Taggedcvalgrind
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.