How can I get valgrind to tell me the address of each non-freed block of memory?
Valgrind tells me function xxx allocated memory which was not freed. Fine. It's proving more difficult than usual to trace however.
To this end I have created numerous:
#ifdef DEBUG
fprintf(stderr, "something happening:%lx\n", (unsigned long)ptr);
#endif
So I just need to match these ptr addresses that are displayed with the addresses of non-freed memory.
How can I get valgrind to tell me the address of ea开发者_C百科ch non-freed block of memory?
I don't believe Memcheck's leak checker supports printing addresses, unfortunately. This is due to the fact it's willing to merge multiple unallocated blocks into one "loss report" if they're similar.
If you don't mind poking around in Memcheck, this functionality should be abled to be added in memcheck/mc_leakcheck.c in the Valgrind source. I'll take a look at it when I get home and post a more detailed location.
Thanks to Falaina for the code location.
For valgrind-3.2.3, location that worked for me was in memcheck/mc_leakcheck.c, lc_scan_memory_WRK function.
Added this after lc_markstack_push_WRK(addr, clique); on line # 472
if (clique != -1) { VG_(printf)("clique %d: %p\n", clique, ptr); }
Here we are twelve years later and this still hasn't been added. The following patch is @Chaim's suggestion to valgrind-3.18.1
diff -uir valgrind-3.18.1/memcheck/mc_leakcheck.c valgrind-3.18.1-new/memcheck/mc_leakcheck.c
--- valgrind-3.18.1/memcheck/mc_leakcheck.c 2021-01-21 15:09:33.000000000 +0000
+++ valgrind-3.18.1-new/memcheck/mc_leakcheck.c 2022-03-08 18:55:24.323996739 +0000
@@ -1166,6 +1166,7 @@
}
} else {
lc_push_if_a_chunk_ptr(addr, clique, cur_clique, is_prior_definite);
+ if (clique != -1) { VG_(printf)("clique %d: %p\n", clique, (void*)ptr); }
}
} else if (0 && VG_DEBUG_LEAKCHECK) {
VG_(printf)("%#lx not valid\n", ptr);
精彩评论