gpt4 book ai didi

linux - 当我在启用 "memory leak"的情况下编译我的程序时,Valgrind 报告了 "-pg"?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:52 25 4
gpt4 key购买 nike

在编译以下简单代码时,启用了 -pg 的 Valgrind 报告了内存泄漏。

#include <iostream>
#include <boost/filesystem.hpp>

#define BOOST_FILESYSTEM_VERSION 3

using boost::filesystem::path;

using namespace std;

int main() {

path ptDir;
ptDir = "/home/foo/bar";

if (true == is_directory((const path &)ptDir)){
cout << "ptDir: " << ptDir << endl;
}
}

完整的编译选项如下。

g++ -pg -g test.cpp -lboost_system -lboost_filesystem

运行valgrind的命令是:

valgrind --gen-suppressions=all --track-origins=yes --error-limit=no --leak-check=full --show-reachable=yes -v --trace-children=yes --track-fds=yes --log-file=vg.log ./a.out

然后 valgrind 给了我一个内存泄漏错误。

==9598== HEAP SUMMARY:
==9598== in use at exit: 4,228 bytes in 1 blocks
==9598== total heap usage: 136 allocs, 135 frees, 17,984 bytes allocated
==9598==
==9598== Searching for pointers to 1 not-freed blocks
==9598== Checked 130,088 bytes
==9598==
==9598== 4,228 bytes in 1 blocks are still reachable in loss record 1 of 1
==9598== at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==9598== by 0x4260F44: monstartup (gmon.c:134)
==9598== by 0xBED6B636: ???
==9598== by 0x4E45504E: ???
==9598==
{
<insert_a_suppression_name_here>
Memcheck:Leak
fun:calloc
fun:monstartup
obj:*
obj:*
}
==9598== LEAK SUMMARY:
==9598== definitely lost: 0 bytes in 0 blocks
==9598== indirectly lost: 0 bytes in 0 blocks
==9598== possibly lost: 0 bytes in 0 blocks
==9598== still reachable: 4,228 bytes in 1 blocks
==9598== suppressed: 0 bytes in 0 blocks
==9598==
==9598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==9598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

这是正确的吗?我正在使用 Ubuntu 12.04/内核 3.2.0-32-generic

最佳答案

Valgrind FAQ :

"still reachable" means your program is probably ok -- it didn't free some 
memory it could have. This is quite common and often reasonable. Don't use
--show-reachable=yes if you don't want to see these reports.

关于linux - 当我在启用 "memory leak"的情况下编译我的程序时,Valgrind 报告了 "-pg"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14018553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com