Valgrind errors even though all heap blocks were freed Valgrind errors even though all heap blocks were freed c c

Valgrind errors even though all heap blocks were freed


This seems obvious ... but it might be worth pointing out that the "no leaks are possible" message does not mean that your program cannot leak; it just means that it did not leak in the configuration under which it was tested.

If I run the following with valgrind with no command line parameters, it informs me that no leaks are possible. But it does leak if I provide a command line parameter.

int main( int argc, char* argv[] ){   if ( argc > 1 )      malloc( 5 );   printf( "Enter any command line arg to cause a leak\n" );}


  1. Yes, you are greatly covered, don'tthink that valgrind easily can missa leak in user code
  2. your error means that you probablyhave a +1 error in indexing an arrayvariable. the lines that valgrindtell you should be accurate, so youshould easily find that, provided you compile all your code with -g
  3. suppressed errors are usually fromsystem libraries, which sometimes have small leaks or undectable things like the state variables of threads. your manual page should list the suppression file that is used by default


Checking for memory leaks is one reason to use valgrind, but I'd say a better reason is to find more serious errors in your code, such as using an invalid array subscript or dereferencing an uninitialized pointer or a pointer to freed memory.

It's good if valgrind tells you that the code paths you exercised while running valgrind didn't result in memory leaks, but don't let that cause you to ignore reports of more serious errors, such as the one you're seeing here.

As other have suggested, rerunning valgrind after compiling with debug information (-g) would be a good next step.