I run Valgrind with the following parameters:
--leak-check=full --show-reachable=yes --leak-resolution=high --num-callers=100 --trace-children=yes
In memory leaks log, I see some error messages with full stack trace up to main, but some messages look like following:
==3956== 1,999,140 (68,796 direct, 1,930,344 indirect) bytes in 5,733 blocks are definitely lost in loss record 8,842 of 8,845 ==3956== at 0x4022AB8: malloc (vg_replace_malloc.c:207) ==3956==
How can I get the full stack trace for these errors?
By default Valgrind's malloc, realloc, etc, return a block whose starting address is 8-byte aligned or 16-byte aligned (the value depends on the platform and matches the platform default).
Look for function names and line numbersIf you compile your program with the -g flag, Valgrind will show you the function names and line numbers where errors occur.
Valgrind is installed on the department machines. To invoke it on an executable called a. out, you simply run the command valgrind ./a.
Valgrind uses dynamic binary instrumentation, so you don't need to modify, recompile or relink your applications. Just prefix your command line with valgrind and everything works.
Getting the full stack trace will require debug symbols for all the libraries/executables that may be involved in a leak (and within the limits set by --num-callers
).
If you're building any of them yourself, you need to specify the -g
flag in gcc (or the relevant flag in any other compiler).
Note that valgrind is not foolproof, and may occasionally miss leaks or be unable to provide full stack traces (especially if you're using threads, or complicated class
implementations).
For libraries without debug information, the stack trace will stop at that library.
For a free tool, valgrind is very good at what it does, but there is a reason places like IBM can sell memory profiles for big money.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With