Valgrind不会抛出任何错误,但并未释放所有堆分配

时间:2016-10-08 03:33:40

标签: c++ memory-management valgrind

这是我用 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 可能会有什么问题吗?

1 个答案:

答案 0 :(得分:4)

valgrind报告的回溯表明,所讨论的内存分配是在应用程序加载的一个共享库的初始化函数中完成的,显然是C ++库本身。

共享库对各种位数据进行一次性分配是很常见的,但是当它们被卸载时却没有明确地解除分配它们。

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

valgrind附带了这种性质的已知分配列表,它被称为“抑制列表”,用于明确抑制有关这些已知一次性分配的报告。

但是,偶尔这些抑制列表会错过一两个分配。