Valgrind 没有抛出错误,但并非所有堆分配都已被释放

Valgrind throws no error but not all heap allocations have been freed

这是我用 Valgrind 执行我的程序后得到的:

1    jscherman@jscherman:~/ClionProjects/algo2-t4-tries$ g++ Set.hpp tests.cpp DiccString.hpp && valgrind --leak-check=yes --show-leak-kinds=all ./a.out                      
2    ==6823== Memcheck, a memory error detector
3    ==6823== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
4    ==6823== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
5    ==6823== Command: ./a.out
6    ==6823== 
7    test_empty_dicc...ok
8    test_copy_constructor...ok
9    test_define_defined...ok
10    test_get..ok
11    test_remove...ok
12    test_remove_tiny...ok
13    test_keys...ok
14    ==6823== 
15    ==6823== HEAP SUMMARY:
16    ==6823==     in use at exit: 72,704 bytes in 1 blocks
17    ==6823==   total heap usage: 282 allocs, 281 frees, 275,300 bytes allocated
18    ==6823== 
19    ==6823== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
20    ==6823==    at 0x4C2DC10: malloc (vg_replace_malloc.c:299)
21    ==6823==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
22    ==6823==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
23    ==6823==    by 0x40105FA: call_init (dl-init.c:30)
24    ==6823==    by 0x40105FA: _dl_init (dl-init.c:120)
25    ==6823==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
26    ==6823== 
27    ==6823== LEAK SUMMARY:
28    ==6823==    definitely lost: 0 bytes in 0 blocks
29    ==6823==    indirectly lost: 0 bytes in 0 blocks
30    ==6823==      possibly lost: 0 bytes in 0 blocks
31    ==6823==    still reachable: 72,704 bytes in 1 blocks
32    ==6823==         suppressed: 0 bytes in 0 blocks
33    ==6823== 
34    ==6823== For counts of detected and suppressed errors, rerun with: -v
35    ==6823== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

似乎没有像输出的最后一行所说的泄漏。然而,我们也有这一行:

17    ==6823==   total heap usage: 282 allocs, 281 frees, 275,300 bytes allocated

我怎么没有任何错误,但仍然有一个分配没有被释放?是我的程序有问题还是 Valgrind 在幕后做了什么?

valgrind 报告的回溯表明,有问题的内存分配是在应用程序加载的共享库之一的初始化函数中进行的,显然是 C++ 库本身。

共享库为各种数据进行一次性分配,但在卸载时懒得显式释放它们是很常见的。

这不包括您自己的代码中的内存泄漏。

valgrind 附带了一份此类已知分配的列表,称为 "suppression list",其明确目的是禁止报告有关这些已知的一次性分配的报告。

但是,偶尔,这些禁止列表确实会遗漏一个或两个分配。