28

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?

4
  • The last released version of Valgrind only supports --num-callers till 50. So, it is not clear which version you are using. The above stack trace looks incomplete. This might be linked to the way your application is compiled (e.g. option -fomit-frame-pointer might make the stack trace more difficult to produce)
    – phd
    Jun 29, 2012 at 22:20
  • Bump. (does stackoverflow "bump" posts?) Same problem. Compiling an application with -g. It shows possible memory leaks with a stack trace staring from malloc(), the function that called malloc(), and then main(), skipping everything in between, including functions within the same file as main(). ?? I see examples online that don't have this 3 stack frame limit..?? Any ideas?
    – Samuel
    Feb 7, 2013 at 22:19
  • I'm using valgrind 3.8.1 on Ubuntu 13.10 and see stack traces that doesn't contain all intermediate calls. No optimization while compiling with gcc (or maybe I need -Og or something...)
    – thoni56
    Sep 6, 2013 at 14:16
  • Have you ensured you have debug symbols installed for all libraries your application links to?
    – stsquad
    Mar 24, 2014 at 10:14

1 Answer 1

31

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 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, is very good at what it does, but there is a reason places like IBM can sell memory profiles for big money.

4
  • 14
    Thanks, in my case I just had to adjust the --num-callers option. The default appears to be 12, which is pretty low for the STL and the like.
    – cib
    Jun 13, 2015 at 20:08
  • 8
    For future viewers, to view the full call stack, run valgrind with --num-callers=<x>, with x being between 1 and 500. Setting it to 500 will lead to the full stack being printed. Feb 12, 2019 at 13:00
  • 2
    Basically, the accepted answer should be these comments ^
    – Dodgie
    Jun 23, 2021 at 0:30
  • I believe the other values are part of the answer - it just appears that most people get tripped up by --num-callers
    – roelofs
    Jun 23, 2021 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.