当我用“-pg”编译我的程序时,Valgrind报告了“内存泄漏”?

时间:2012-12-24 07:39:57

标签: linux memory profiling valgrind memory-leaks

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

#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 / Kernel 3.2.0-32-generic

2 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:1)

分配的事实来自: by 0x4260F44: monstartup (gmon.c:134) 表明它是-pg的副作用 - 这是你无能为力的任何事情。不要混用-pg和valgrind是我的建议。