我有一个非常简单的C++程序。
#include <iostream>
int main()
{
std::cout << "HI" << std::endl;
return 0;
}
我用c++ --std=c++11 leak.cpp
命令在Mac上编译了这段代码。
当我使用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
会导致内存泄漏?
发布于 2015-05-26 20:22:13
可能是泄露报告中的假阳性。Valgrind只能如此聪明;您的标准库实现正在使用Valgrind没有特殊情况的某些自由。
我更担心的是,为什么这个小程序要执行508次分配,总共45,074字节。
https://stackoverflow.com/questions/30468149
复制相似问题