我正在运行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