Valgrind报告内存肯定丢失没有错误

Valgrind reporting memory definitely lost without errors

我正在测试 C 代码的内存泄漏,但似乎找不到泄漏源,因为有 0 个错误。 Valgrind 报告存在(非常重要的)内存泄漏:

==30492== Memcheck, a memory error detector
==30492== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30492== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright 
info
==30492== Command: ./a.out --leak-check=full --track-origins=yes
==30492== 
(This is where the input and output cases are displayed, which are a lot)
==30492== 
==30492== HEAP SUMMARY:
==30492==     in use at exit: 39,155 bytes in 167 blocks
==30492==   total heap usage: 380 allocs, 213 frees, 53,426 bytes allocated
==30492== 
==30492== LEAK SUMMARY:
==30492==    definitely lost: 20,480 bytes in 2 blocks
==30492==    indirectly lost: 2,064 bytes in 1 blocks
==30492==      possibly lost: 0 bytes in 0 blocks
==30492==    still reachable: 348 bytes in 9 blocks
==30492==         suppressed: 16,263 bytes in 155 blocks
==30492== Rerun with --leak-check=full to see details of leaked memory
==30492== 
==30492== For counts of detected and suppressed errors, rerun with: -v
==30492== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

代码写在几个文件中,由数百行组成,所以在这里发布可能有点多。谁能解释这里可能是什么问题?还是您需要查看实际代码才能给出答案?我在 valgrind 上只能找到很少的文档,而且很困在这里。

(valgrind 建议使用 --leak-check=full 重新运行,但这是我为获得此输出所做的)

可能会出现误报(例如,在共享库初始化程序中,或者像 libcrypto.so 这样确实会泄漏一些分配的东西)。

但是,您应该经常检查 - 很可能您忘记了一些分配。

在你的输出中,我们可以看到:

 Command: ./a.out --leak-check=full --track-origins=yes`

这表明您已通过以下方式调用了 valgrind:

valgrind ./a.out --leak-check=full --track-origins=yes

应该使用这个:

valgrind --leak-check=full --track-origins=yes ./a.out

如果您发现无法控制的泄漏(或其他诊断),因为它在 third-party 库内部,您可以创建 suppression file