8

我看过一些关于 valgrind 的帖子,但没有一篇文章帮助我理解对 valgrind 输出的解释。我用 valgrind 运行了两个程序(都有内存泄漏)

测试 1 的样本输出

==20422== LEAK SUMMARY:
==20422== definitely lost: 448 bytes in 3 blocks
==20422== indirectly lost: 786,460 bytes in 1 blocks
==20422== possibly lost: 1,576,052 bytes in 46 blocks
==20422== still reachable: 1,077,107 bytes in 2,333 blocks
==20422== suppressed: 0 bytes in 0 blocks
==20422== Rerun with --leak-check=full to see details of leaked memory
==20422==
==20422== For counts of detected and suppressed errors, rerun with: -v
==20422== ERROR SUMMARY: 98307 errors from 5 contexts (suppressed: 2 from 2)
Killed

测试 2 的输出

==20875== HEAP SUMMARY:
==20875== in use at exit: 1,059,198 bytes in 2,047 blocks
==20875== total heap usage: 3,019 allocs, 972 frees, 4,496,090 bytes allocated
==20875==
==20875== LEAK SUMMARY:
==20875== definitely lost: 328 bytes in 2 blocks
==20875== indirectly lost: 0 bytes in 0 blocks
==20875== possibly lost: 1,600 bytes in 5 blocks
==20875== still reachable: 1,057,270 bytes in 2,040 blocks
==20875== suppressed: 0 bytes in 0 blocks
==20875== Rerun with --leak-check=full to see details of leaked memory
==20875==
==20875== For counts of detected and suppressed errors, rerun with: -v
==20875== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

如果您查看错误摘要,一个输出说有错误,另一个说没有错误(最终两者都有泄漏)

我的问题是——valgrind 中的错误是什么?(valgrind 手册对我没有帮助)

4

1 回答 1

6

内存泄漏不被视为错误,它们不会影响程序的逻辑。

错误是诸如无效读取和写入之类的事情。

根据评论更新:测试 1 中的无效读取和写入针对 5 个不同的内存区域,访问了 98307 次。

查看泄漏,测试 1 中的大量间接损失可能表明链接数据结构仅删除了根。

测试 2 中的泄漏还不错。正如 valgrind 建议使用 --leak-check=full 重新运行的那样,这应该表明是哪位代码导致了问题。

可以在此处找到错误的完整说明

于 2013-07-01T10:29:41.093 回答