Why is Valgrind stating that my implementation of std::map produces a memory leak?

前端 未结 2 1599
既然无缘
既然无缘 2021-01-14 14:14

Valgrind is outputting the following:

==14446== 2,976 (176 direct, 2,800 indirect) bytes in 2 blocks are definitely lost in loss record 23 of 33
==14446==            


        
2条回答
  •  自闭症患者
    2021-01-14 14:58

    It's probably because of a pool allocator. From Valgrind FAQ:

    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.

    Read more at: Valgrind Faq

    I may be wrong, as I'm in a hurry and I can't analyse your code.

提交回复
热议问题