Valgrind 检测到一个普通代码的未释放堆内存使用情况
Valgrind detects unfreed heap memory usage for a trivial code
我的 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 的错误吗?
这是由于 C++ 标准库的工作方式所致。容器分配内存块(称为池)并在内部管理它们。他们基本上使用自己的内存管理器,而不是依赖系统的内存管理器。因此,当一个对象被销毁时,它的内存被内部分配器释放以供重用,但不会返回给操作系统。
这在 valgrind 的常见问题解答中也有描述 here。
更概括地说,valgrind 是一个非常有用的工具,但您不应该以 0 泄漏为目标,而应该理解它的报告并找到表明代码中存在问题的泄漏。
我在 Ubuntu 19.04 下使用 valgrind 3.14.0,我没有得到任何检测。我 运行 有 --leak-check=full
和没有。也许它只是一些版本的 valgrind。
我的 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 的错误吗?
这是由于 C++ 标准库的工作方式所致。容器分配内存块(称为池)并在内部管理它们。他们基本上使用自己的内存管理器,而不是依赖系统的内存管理器。因此,当一个对象被销毁时,它的内存被内部分配器释放以供重用,但不会返回给操作系统。
这在 valgrind 的常见问题解答中也有描述 here。
更概括地说,valgrind 是一个非常有用的工具,但您不应该以 0 泄漏为目标,而应该理解它的报告并找到表明代码中存在问题的泄漏。
我在 Ubuntu 19.04 下使用 valgrind 3.14.0,我没有得到任何检测。我 运行 有 --leak-check=full
和没有。也许它只是一些版本的 valgrind。