4.1. | My threaded server process runs unbelievably slowly on Valgrind. So slowly, in fact, that at first I thought it had completely locked up. |
We are not completely sure about this, but one possibility is that laptops with power management fool Valgrind's timekeeping mechanism, which is (somewhat in error) based on the x86 RDTSC instruction. A "fix" which is claimed to work is to run some other cpu-intensive process at the same time, so that the laptop's power-management clock-slowing does not kick in. We would be interested in hearing more feedback on this. Another possible cause is that versions prior to 1.9.6 did not support threading on glibc 2.3.X systems well. Hopefully the situation is much improved with 1.9.6 and later versions. |
|
4.2. | My program uses the C++ STL and string classes. Valgrind reports 'still reachable' memory leaks involving these classes at the exit of the program, but there should be none. |
First of all: relax, it's probably not a bug, but a feature. Many implementations of the C++ standard libraries use their own memory pool allocators. Memory for quite a number of destructed objects is not immediately freed and given back to the OS, but kept in the pool(s) for later re-use. The fact that the pools are not freed at the exit() of the program cause Valgrind to report this memory as still reachable. The behaviour not to free pools at the exit() could be called a bug of the library though. Using gcc, you can force the STL to use malloc and to free memory as soon as possible by globally disabling memory caching. Beware! Doing so will probably slow down your program, sometimes drastically.
There are other ways to disable memory pooling: using the malloc_alloc template with your objects (not portable, but should work for gcc) or even writing your own memory allocators. But all this goes beyond the scope of this FAQ. Start by reading http://gcc.gnu.org/onlinedocs/libstdc++/ext/howto.html#3 if you absolutely want to do that. But beware:
|
|
4.3. | The stack traces given by Memcheck (or another tool) aren't helpful. How can I improve them? |
If they're not long enough, use --num-callers to make them longer. If they're not detailed enough, make sure you are compiling with -g to add debug information. And don't strip symbol tables (programs should be unstripped unless you run 'strip' on them; some libraries ship stripped). Also, for leak reports involving shared objects, if the shared object is unloaded before the program terminates, Valgrind will discard the debug information and the error message will be full of ??? entries. The workaround here is to avoid calling dlclose() on these shared objects. Also, -fomit-frame-pointer and -fstack-check can make stack traces worse. Some example sub-traces: With debug information and unstripped (best): Invalid write of size 1 at 0x80483BF: really (malloc1.c:20) by 0x8048370: main (malloc1.c:9) With no debug information, unstripped: Invalid write of size 1 at 0x80483BF: really (in /auto/homes/njn25/grind/head5/a.out) by 0x8048370: main (in /auto/homes/njn25/grind/head5/a.out) With no debug information, stripped: Invalid write of size 1 at 0x80483BF: (within /auto/homes/njn25/grind/head5/a.out) by 0x8048370: (within /auto/homes/njn25/grind/head5/a.out) by 0x42015703: __libc_start_main (in /lib/tls/libc-2.3.2.so) by 0x80482CC: (within /auto/homes/njn25/grind/head5/a.out) With debug information and -fomit-frame-pointer: Invalid write of size 1 at 0x80483C4: really (malloc1.c:20) by 0x42015703: __libc_start_main (in /lib/tls/libc-2.3.2.so) by 0x80482CC: ??? (start.S:81) A leak error message involving an unloaded shared object: 84 bytes in 1 blocks are possibly lost in loss record 488 of 713 at 0x1B9036DA: operator new(unsigned) (vg_replace_malloc.c:132) by 0x1DB63EEB: ??? by 0x1DB4B800: ??? by 0x1D65E007: ??? by 0x8049EE6: main (main.cpp:24) |
|
4.4. | The stack traces given by Memcheck (or another tool) seem to have the wrong function name in them. What's happening? |
Occasionally Valgrind stack traces get the wrong function names. This is caused by glibc using aliases to effectively give one function two names. Most of the time Valgrind chooses a suitable name, but very occasionally it gets it wrong. Examples we know of are printing 'bcmp' instead of 'memcmp', 'index' instead of 'strchr', and 'rindex' instead of 'strrchr'. |
|
5.1. | I try running "valgrind --tool=memcheck my_program" and get Valgrind's startup message, but I don't get any errors and I know my program has errors. |
There are two possible causes of this. First, by default, Valgrind only traces the top-level process. So if your program spawns children, they won't be traced by Valgrind by default. Also, if your program is started by a shell script, Perl script, or something similar, Valgrind will trace the shell, or the Perl interpreter, or equivalent. To trace child processes, use the --trace-children=yes option. If you are tracing large trees of processes, it can be less disruptive to have the output sent over the network. Give Valgrind the flag --log-socket=127.0.0.1:12345 (if you want logging output sent to port 12345 on localhost). You can use the valgrind-listener program to listen on that port: valgrind-listener 12345 Obviously you have to start the listener process first. See the Manual: Directing output to file for more details. Second, if your program is statically linked, most Valgrind tools won't work as well, because they won't be able to replace certain functions, such as malloc(), with their own versions. A key indicator of this is if Memcheck says: No malloc'd blocks -- no leaks are possible when you know your program calls malloc(). The workaround is to avoid statically linking your program. |
|
5.2. |
Why doesn't Memcheck find the array overruns in this program? int static[5]; int main(void) { int stack[5]; static[5] = 0; stack [5] = 0; return 0; } |
Unfortunately, Memcheck doesn't do bounds checking on static or stack arrays. We'd like to, but it's just not possible to do in a reasonable way that fits with how Memcheck works. Sorry. |
|
6.1. | I tried writing a suppression but it didn't work. Can you write my suppression for me? |
Yes! Use the --gen-suppressions=yes feature to spit out suppressions automatically for you. You can then edit them if you like, eg. combining similar automatically generated suppressions using wildcards like '*'. If you really want to write suppressions by hand, read the manual carefully. Note particularly that C++ function names must be _mangled_. |
|
6.2. | With Memcheck/Addrcheck's memory leak detector, what's the difference between "definitely lost", "possibly lost", "still reachable", and "suppressed"? |
The details are in the Manual: Memory leak detection. In short:
|
|
Please read all of this section before posting.
If you think an answer is incomplete or inaccurate, please e-mail valgrind@valgrind.org.
Read the appropriate section(s) of the Manual(s): Valgrind Documentation.
Read the Distribution Documents.
Search the valgrind-users mailing list archives, using the group name gmane.comp.debugging.valgrind.
Only when you have tried all of these things and are still stuck, should you post to the valgrind-users mailing list. In which case, please read the following carefully. Making a complete posting will greatly increase the chances that an expert or fellow user reading it will have enough information and motivation to reply.
Make sure you give full details of the problem, including the full output of valgrind -v, if applicable. Also which Linux distribution you're using (Red Hat, Debian, etc) and its version number.
You are in little danger of making your posting too long unless you include large chunks of valgrind's (unsuppressed) output, so err on the side of giving too much information.
Clearly written subject lines and message bodies are appreciated, too.
Finally, remember that, despite the fact that most of the community are very helpful and responsive to emailed questions, you are probably requesting help from unpaid volunteers, so you have no guarantee of receiving an answer.