2

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++.

Noam Elul
  • 31
  • 1
  • 3
  • "Still reachable" means you haven't deallocated a block of memory before exiting, but had a pointer to it. It doesn't mean the memory is available after you exit the program. It doesn't matter *where* the variable is that was assigned the memory allocation. – Weather Vane Sep 22 '21 at 19:10
  • 1
    Does this answer your question? [Still reachable in valgrind](https://stackoverflow.com/questions/4406402/still-reachable-in-valgrind) – Stephen Newell Sep 22 '21 at 19:10
  • It depends if "some memory" is stack or heap allocated. – Allan Wind Sep 22 '21 at 19:51
  • 1
    If you `abort`, the program ends with an absolute minimum of clean up. The OS will reclaim the resources it knows about, but anything fancy, like a C++ destructor repositioning a robot arm to a safe default, will go unexecuted. `exit` does a bit more and flushes streams, calls destructors of objects with static storage duration, but doesn't unwind the stack, an often-important thing to do in C++. In either case, Valgrind will likely be quite upset and log many memory errors. – user4581301 Sep 22 '21 at 20:41
  • I have had the OS leave a shared memory block locked when the program exits so that no other program can use it. Having to delete the shared memory block and recreate it was the only way. Registering your cleanup functions with https://en.cppreference.com/w/cpp/utility/program/atexit helps if you can't use RAII. RAII is obviously the best thing you can do to make sure everything is cleaned up properly. – Jerry Jeremiah Sep 22 '21 at 21:13
  • @WeatherVane That's part of my question. If I exit by returning from main, has the call stack from main been popped off yet when Valgrind decides if memory is still reachable? Things like exit handlers get called after main exits, so it wouldn't be unreasonable to think that Valgrind is only checking if memory is reachable after all of the local variables from main no longer exist. – Noam Elul Sep 23 '21 at 03:42

1 Answers1

0

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".

Noam Elul
  • 31
  • 1
  • 3