【发布时间】:2012-01-27 17:11:37
【问题描述】:
我以前使用过 valgrind,它非常有帮助。我最近设置了一个开发环境并再次开始使用 valgrind。这次它发现没有丢失的记忆!即使我 malloc 一些内存然后用 CTRL-C 中断程序,我也会得到下面的转储。有人可以解释发生了什么吗?
困惑....
==2489== HEAP SUMMARY:
==2489== in use at exit: 314,145 bytes in 585 blocks
==2489== total heap usage: 1,410 allocs, 825 frees, 2,025,829 bytes allocated
==2489==
==2489== LEAK SUMMARY:
==2489== definitely lost: 0 bytes in 0 blocks
==2489== indirectly lost: 0 bytes in 0 blocks
==2489== possibly lost: 0 bytes in 0 blocks
==2489== still reachable: 314,145 bytes in 585 blocks
==2489== suppressed: 0 bytes in 0 blocks
==2489== Reachable blocks (those to which a pointer was found) are not shown.
==2489== To see them, rerun with: --leak-check=full --show-reachable=yes
==2489==
==2489== For counts of detected and suppressed errors, rerun with: -v
==2489== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
【问题讨论】:
-
如果程序终止并且您仍然有指向它的指针,内存不会丢失。它显示在“仍然可访问”下,因为它在程序结束时仍然可访问。
-
ctrl+c 发送一个 SIGINT,它基本上会中断您的程序继续执行。这就是为什么你得到“仍然可以访问”的原因,因为所有内存都已分配但未释放。如果你使用 valgrind 建议的标志,主要是
--show-reachable=yes,那么它会给出位置。 -
@BlackJack:请作为答案,我会投赞成票,这比已经存在的任何一个都更好地解释它。