Home >Backend Development >C++ >Why Does Valgrind Report Still Reachable Memory with a Trivial Program Using ``?
Valgrind: Memory still reachable with trivial program using
This question is about a Valgrind warning about still reachable memory in a trivial program that includes the
Problem:
A trivial program that includes the
#include <iostream> int main() { return 0; }
Answer:
The Valgrind warning is not a bug, but a feature. Many implementations of the C standard library use their own memory pool allocators. Memory for destructed objects is not immediately freed and given back to the OS, but kept in the pool for later reuse. The memory pools are also not freed when the program exits, causing Valgrind to report the memory as still reachable.
Solution:
There are several ways to disable pool caching and force the STL to use malloc and free memory as soon as possible:
g++ -D__USE_MALLOC ...
export GLIBCPP_FORCE_NEW=1
export GLIBCXX_FORCE_NEW=1
Additional Notes:
The above is the detailed content of Why Does Valgrind Report Still Reachable Memory with a Trivial Program Using ``?. For more information, please follow other related articles on the PHP Chinese website!