我正在運行valgrind 3.5.0來嘗試和壓縮我的程序中的內存泄漏。 我調用它像這樣:混淆Valgrind輸出:間接丟失塊但沒有錯誤?
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes
我的程序完成後的valgrind報告說
==22926==
==22926== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==22926== malloc/free: in use at exit: 20,862 bytes in 425 blocks.
==22926== malloc/free: 25,361 allocs, 24,936 frees, 772,998 bytes allocated.
==22926== For counts of detected errors, rerun with: -v
==22926== searching for pointers to 425 not-freed blocks.
==22926== checked 91,884 bytes.
儘管告訴我,有0個錯誤,我關注的是分配和釋放的數量不匹配。更多 令人擔憂的仍是:
==22926== LEAK SUMMARY:
==22926== definitely lost: 68 bytes in 1 blocks.
==22926== indirectly lost: 20,794 bytes in 424 blocks.
==22926== possibly lost: 0 bytes in 0 blocks.
==22926== still reachable: 0 bytes in 0 blocks.
==22926== suppressed: 0 bytes in 0 blocks.
有額外的輸出,關於這似乎是一個泄漏:
==22926== 20,862 (68 direct, 20,794 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 17
==22926== at 0x40269EE: operator new(unsigned int) (vg_replace_malloc.c:224)
==22926== by 0x807960B: OneTwoThree::OneTwoThree(Scenario const*) (onetwothree.cc:22)
==22926== by 0x804DD69: main (scsolver.cpp:654)
在OneTwoThree的構造有問題的線,我有以下:
OneTwoThree::OneTwoThree (const Scenario* scenario) :
Choice("123", scenario, new Solution (scenario->name(), scenario)),
seen_(new bool [sol_->numVisits()])
{
}
後,在析構函數,seen_被刪除像這樣:
OneTwoThree::~OneTwoThree()
{
delete [] seen_;
}
沒有與seen_關聯的內存重新分配;我只在 運行我的程序期間將布爾轉換爲真/假。
我在這裏看不到泄漏,我不明白valgrind想告訴我什麼。我一直在閱讀通過 valgrind手冊(具體來說,this),但 我不是很開明。
任何人都可以幫我提供這個輸出嗎?
有沒有可能'Solution'對象*本身*永遠不會被破壞? – Artelius 2009-11-04 03:07:53
Choice類是否取消了它在構造函數中獲取的指針?看起來更好的設計是將指針所有權指向同一個類 - 如果OneTwoThree分配內存,則同一個實例應該取消分配它。 – 2009-11-04 03:17:20
Choice構造函數和它的親戚是否完全釋放了一切? – 2009-11-04 03:21:05