2012-12-24 61 views
1

編譯以下簡單代碼時,我收到了Valgrind報告的內存泄漏,並啓用了-pg。當我使用「-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泛型

+0

你應該「相信」valgrind,但如果性能分析功能沒有釋放所有的內存,我不會感到驚訝。嘗試使用「-pg」進行編譯,看看是否有所作爲 –

回答

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

的分配與來自這樣的事實: by 0x4260F44: monstartup (gmon.c:134) 表明它是-pg的副作用 - 這沒有什麼可以做的。不要混用-pg和valgrind是我的建議。

相關問題