的代码是相当简单:Valgrind的示出的std ::矢量<>的alloc的倍比自由多,但没有内存泄漏
#include <vector>
int main() {
std::vector<int> v;
}
然后我构建和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)
问题是双重的:
(1)“总堆使用率”表示有1个alloc和0空闲。我认为1分配是因为std :: vector实例需要堆中的内存块。没关系;但为什么它在销毁期间不释放内存? (2)如果它不释放它,为什么“LEAK SUMMARY”中没有内存泄漏?
(3)顺便说一句,每行之前的==8511==
是什么意思? (不过我可以在手册中查到它,你不必回答)
谢谢!
您的问题与C完全无关。 – StoryTeller
看起来像Valgrind 3.11试图解决这个问题:https://bugzilla.redhat.com/show_bug.cgi?id=1312647 –
有很多东西可能分配C++。它必须为stdin,stdout和stderr创建文件句柄和缓冲区,并且它可能会创建一个内存池。尝试删除'std :: vector <>'看看valgrind说什么 – gman