我有一个非常简单的C++程序。
#include <iostream>
int main()
{
std::cout << "HI" << std::endl;
return 0;
}
我在Mac上使用
c++ --std=c++11 leak.cpp
命令进行编译。当我使用
valgrind --leak-check=full ./a.out
调试时,得到以下输出:==2187== HEAP SUMMARY:
==2187== in use at exit: 38,906 bytes in 429 blocks
==2187== total heap usage: 508 allocs, 79 frees, 45,074 bytes allocated
==2187==
==2187== LEAK SUMMARY:
==2187== definitely lost: 0 bytes in 0 blocks
==2187== indirectly lost: 0 bytes in 0 blocks
==2187== possibly lost: 0 bytes in 0 blocks
==2187== still reachable: 4,096 bytes in 1 blocks
==2187== suppressed: 34,810 bytes in 428 blocks
==2187== Reachable blocks (those to which a pointer was found) are not shown.
==2187== To see them, rerun with: --leak-check=full --show-leak-kinds=all
原来有4096个字节“仍然可以到达”。如果删除
cout
语句,则不再有“仍然可访问”的字节。为什么输出到
std::cout
会导致内存泄漏? 最佳答案
在泄漏报告中可能是误报。 Valgrind只能如此聪明。您的标准库实现拥有Valgrind没有特殊情况的某些自由。
我会更担心找出这个小程序为什么要执行508分配,总共45,074字节。
关于c++ - std::cout导致内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30468149/