我正在运行valgrind 3.5.0来尝试压缩程序中的内存泄漏。 我这样调用它:
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_相关的内存重新分配;我只是在整个过程中将bool翻转为真/假 运行我的程序。
我在这里看不到泄漏,我不明白valgrind试图告诉我什么。我一直在阅读 valgrind手册(具体来说,this)但是 我不是很开明。
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
OP的评论者被发现;从未删除在构造函数中创建的Solution对象。我已经修复了这个令人震惊的疏忽,并且摆脱了丑陋的代码,在对象的构造函数之外创建了新对象。
感谢Artelius,Nikolai和Jonathan!