2017-08-06 31 views
2

的代码是相当简单: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==是什么意思? (不过我可以在手册中查到它,你不必回答)

谢谢!

+0

您的问题与C完全无关。 – StoryTeller

+0

看起来像Valgrind 3.11试图解决这个问题:https://bugzilla.redhat.com/show_bug.cgi?id=1312647 –

+0

有很多东西可能分配C++。它必须为stdin,stdout和stderr创建文件句柄和缓冲区,并且它可能会创建一个内存池。尝试删除'std :: vector <>'看看valgrind说什么 – gman

回答

4

报告的内存仍在被C++运行时使用。你不需要担心它。 Valgrind的FAQ有an entry关于这个问题:

首先:放松,这可能不是一个错误,而是一个功能。 C++标准库的许多实现都使用它们自己的内存池分配器。相当多的被破坏对象的内存不会立即释放并返回到操作系统,而是保存在池中供以后重新使用。

+0

谢谢。哇,一个空的矢量在堆中占用71KB .. – user8385554

+1

@ user8385554 - 它不是真正使用内存的“矢量”,而是libstdC++运行时。这里有一个[描述发生什么的bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65434)。请注意,如果删除vector v,它将消失,但这只是因为链接器根本不包含'libstdC++',因为没有任何需要,所以全局init永远不会发生。如果你用'-O'编译,“泄漏”也会消失,因为整个事情都被优化了。 – BeeOnRope

2

(1)正确实施的默认构造std::vector不分配。 Esp不是72k。尝试使用--leak-check=full --track-origins=yes运行,也许它显示分配的起源

(2)它说,看看:仍然可以到达。内存还没有泄漏,因为仍有一个句柄(例如:指针)指向它。

(3)它是应用程序的进程ID。

+0

感谢您指出选项 – user8385554

相关问题