我试图理解valgrind的输出执行它如下: valgrind --leak-check = yes" someprogram"
输出在这里:
==30347==
==30347== HEAP SUMMARY:
==30347== in use at exit: 126,188 bytes in 2,777 blocks
==30347== total heap usage: 4,562 allocs, 1,785 frees, 974,922 bytes
allocated
==30347==
==30347== LEAK SUMMARY:
==30347== definitely lost: 0 bytes in 0 blocks
==30347== indirectly lost: 0 bytes in 0 blocks
==30347== possibly lost: 0 bytes in 0 blocks
==30347== still reachable: 126,188 bytes in 2,777 blocks
==30347== suppressed: 0 bytes in 0 blocks
==30347== Reachable blocks (those to which a pointer was found) are
not shown.
==30347== To see them, rerun with: --leak-check=full --show-reachable=yes
==30347==
==30347== For counts of detected and suppressed errors, rerun with: -v
==30347== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
根据输出,没有丢失的字节,但似乎仍有可到达的块。我有内存泄漏吗?
提前感谢您的帮助。
答案 0 :(得分:1)
没有
您最关心的是无法访问的块。你在这里看到的是,有些活跃的变量仍然是指向"在可达到的内存块。它们仍在范围内。
例如,一个无法访问的块将是您已动态分配的内存,在一段时间内使用,然后即使程序仍在执行,所有对它的引用也已超出范围。由于您不再有任何指向它们的句柄,因此它们现在无法恢复,从而造成内存泄漏。
以下是Valgrind文档的引用:
"仍然可以到达"意味着你的程序可能没问题 - 它没有释放它可能拥有的内存。这很常见,而且往往是合理的。如果您不想看到这些报告,请不要使用--show-reachable = yes。