Valgrind - What pointers are considered valid at program exit to mark memory as "still reachable"?

Viewed 97

If the only pointer to some memory is a local variable in my main function, and I exit the program by returning from main, is that memory considered "still reachable"?

Similarly, if I exit by calling exit in some function, is memory that is only pointed to by a local variable in that function (or a function that called it) considered "still reachable"? What about if I exit through other methods like abort or _exit?

I'm working in C, but I would also be interested in hearing if any of this works any differently in C++.

1 Answers

I had some time, so I decided to check this myself.

If you return from main, Valgrind considers memory that was only pointed to by local variables in main "definitely lost", presumably because all of main's local variables are popped off the call stack before the program exits.

On the other hand if you exit by calling exit, _Exit, abort, or pthread_exit (even if you call them from main), pointers inside local variables in the main function are still considered to exist, so the memory is considered "still reachable".

Related