gpt4 book ai didi

c++ - 令人困惑的 Valgrind 输出 : indirectly lost blocks but no errors?

转载 作者:行者123 更新时间:2023-11-30 04:40:11 25 4
gpt4 key购买 nike

我正在运行 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 )但是我开悟不多。

谁能帮我理解这个输出?

最佳答案

OP 的评论者是正确的;在构造函数中创建的 Solution 对象从未被删除。我已经修复了令人震惊的疏忽,并摆脱了在负责它们的对象的构造函数之外创建新对象的丑陋代码。

谢谢 Artelius、Nikolai 和 Jonathan!

关于c++ - 令人困惑的 Valgrind 输出 : indirectly lost blocks but no errors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1671469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com