我的valgrind告诉我,它为最琐碎的C ++代码找到了未释放的堆内存。
我的代码如下所示:
#include <iostream>
#include <string>
int main() {
std::cout << "Hello!!!!" << std::endl;
return 0;
}
valgrind的结果在这里:
==12455== HEAP SUMMARY:
==12455== in use at exit: 72,704 bytes in 1 blocks
==12455== total heap usage: 2 allocs, 1 frees, 73,728 bytes allocated
==12455==
==12455== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==12455== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12455== by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==12455== by 0x40106C9: call_init.part.0 (dl-init.c:72)
==12455== by 0x40107DA: call_init (dl-init.c:30)
==12455== by 0x40107DA: _dl_init (dl-init.c:120)
==12455== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==12455==
==12455== LEAK SUMMARY:
==12455== definitely lost: 0 bytes in 0 blocks
==12455== indirectly lost: 0 bytes in 0 blocks
==12455== possibly lost: 0 bytes in 0 blocks
==12455== still reachable: 72,704 bytes in 1 blocks
==12455== suppressed: 0 bytes in 0 blocks
==12455==
==12455== For counts of detected and suppressed errors, rerun with: -v
==12455== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
这是valgrind的错误吗?
答案 0 :(得分:0)
这是由于C ++标准库的工作方式所致。容器分配内存块(称为池)并在内部进行管理。他们基本上使用自己的内存管理器,而不是依靠系统的内存管理器。因此,当一个对象被销毁时,它的内存将由内部分配器释放以供重用,但不会交还给操作系统。
这在valgrind的常见问题解答here中也有描述。
为了进一步概括,valgrind是一个非常有用的工具,但是您不应该针对0泄漏,而应该了解其报告并查找表明代码中有问题的泄漏。
答案 1 :(得分:0)
我在Ubuntu 19.04下使用valgrind 3.14.0,但没有得到任何检测。我和--leak-check=full
一起跑步,没有。也许只是valgrind的某些版本。