2

在编译以下简单代码时,启用了 -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

4

2 回答 2

2

Valgrind 常见问题解答

"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.
于 2012-12-24T08:28:05.337 回答
2

allocatio 来自的事实 by 0x4260F44: monstartup (gmon.c:134) 表明它是 -pg 的副作用-您无能为力。不要混合 -pg 和 valgrind 是我的建议。

于 2012-12-24T09:21:53.920 回答