Lee*_*hai 3 c++ valgrind memory-leaks stl
代码相当简单:
#include <vector>
int main() {
std::vector<int> v;
}
Run Code Online (Sandbox Code Playgroud)
然后我用 Valgrind 构建并运行它:
g++ test.cc && valgrind ./a.out
==8511== Memcheck, a memory error detector
==8511== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8511== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8511== Command: ./a.out
==8511==
==8511==
==8511== HEAP SUMMARY:
==8511== in use at exit: 72,704 bytes in 1 blocks
==8511== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==8511==
==8511== LEAK SUMMARY:
==8511== definitely lost: 0 bytes in 0 blocks
==8511== indirectly lost: 0 bytes in 0 blocks
==8511== possibly lost: 0 bytes in 0 blocks
==8511== still reachable: 72,704 bytes in 1 blocks
==8511== suppressed: 0 bytes in 0 blocks
==8511== Rerun with --leak-check=full to see details of leaked memory
==8511==
==8511== For counts of detected and suppressed errors, rerun with: -v
==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)
这个问题有两个方面:
(1)“总堆使用量”表示有1个alloc和0个free。我假设 1 alloc 是因为 std::vector 实例需要堆中的内存块。没关系; 但是为什么它在销毁过程中不释放内存?
(2) 而且,如果它不释放它,为什么“泄漏摘要”中没有内存泄漏?
(3) 顺便说一句,==8511==每行之前是什么意思?(不过,我可以在手册中查找。您不必回答这个问题)
谢谢!
报告的内存仍在被 C++ 运行时使用。您无需担心。Valgrind 的 FAQ 有一个关于这个问题的条目:
首先:放松,这可能不是一个错误,而是一个功能。C++ 标准库的许多实现使用它们自己的内存池分配器。相当多的被破坏对象的内存不会立即释放并返回给操作系统,而是保留在池中供以后重用。
| 归档时间: |
|
| 查看次数: |
903 次 |
| 最近记录: |