Valgrind 报告共享对象内存泄漏

问题描述 投票:0回答:3

我有一个 C 程序,可以使用 libmpg123libao 播放 mp3 文件。我使用 Valgrind 通过以下命令检测内存泄漏和其他潜在问题:

valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt ./pac.out

valgrind-out.txt
中,我可以看到很多问题,例如以下问题:

==14751== 
==14751== 897 bytes in 127 blocks are possibly lost in loss record 143 of 192
==14751==    at 0x48407B4: malloc (vg_replace_malloc.c:381)
==14751==    by 0x66AD554: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B02E4: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B09DA: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B413A: snd_config_update_r (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B474C: snd_config_update (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x665465C: ???
==14751==    by 0x6654CD1: ???
==14751==    by 0x48F1853: ??? (in /usr/lib/x86_64-linux-gnu/libao.so.4.1.1)
==14751==    by 0x10BE15: play_sound (in /root/bin/public-address-client/pac.out)
==14751==    by 0x10C08F: handle_sound_name_queue (in /root/bin/public-address-client/pac.out)
==14751==    by 0x4995FD3: start_thread (pthread_create.c:442)
==14751== 
==14751== 920 bytes in 56 blocks are possibly lost in loss record 144 of 192
==14751==    at 0x48407B4: malloc (vg_replace_malloc.c:381)
==14751==    by 0x66AD554: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66AD8DD: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B035C: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B09DA: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B413A: snd_config_update_r (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B474C: snd_config_update (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x665465C: ???
==14751==    by 0x6654CD1: ???
==14751==    by 0x48F1853: ??? (in /usr/lib/x86_64-linux-gnu/libao.so.4.1.1)
==14751==    by 0x10BE15: play_sound (in /root/bin/public-address-client/pac.out)
==14751==    by 0x10C08F: handle_sound_name_queue (in /root/bin/public-address-client/pac.out)
==14751== 
==14751== 936 bytes in 13 blocks are possibly lost in loss record 145 of 192
==14751==    at 0x48455EF: calloc (vg_replace_malloc.c:1328)
==14751==    by 0x66ACAB1: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66AD974: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B1109: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B0582: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B060C: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B09DA: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B0CBB: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B0D72: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B2AD4: snd_config_hook_load (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B39DA: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751==    by 0x66B3EF6: ??? (in /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0)
==14751== 

正如 Valgrind 所说,可能的泄漏发生在

libasound.so.2.0.0
,这是否意味着我无法从我这边修复它?

如果您想知道,可以在

这里
找到日志中显示的play_sound()功能。

c memory-leaks valgrind
3个回答
1
投票

mpg123
库创建者还提供了抑制文件这一事实意味着很可能will存在来自库内部的泄漏,并且这些泄漏被认为是安全的并且应该忽略(抑制) .

  1. 从库创建者处下载官方 抑制文件:https://mpg123.org/trunk/valgrind.sup
  2. 使用 选项重新运行
    valgrind

0
投票

您可以找到这些库的源代码(如果有),在打开调试信息(-g)的情况下在本地编译它们,然后链接到您的项目。现在,Valgrind 可以告诉您这些库中的泄漏位置,即您将看到实际的堆栈,而不是大量的“??”。如果问题不在你这边(例如,你忘记从库中释放你应该拥有的内存),那么你可以自己修复错误并将修复程序提供给库的作者,或者告诉他们问题并要求他们解决。


0
投票

有几个可能的原因

  1. 库提供了清理函数,但你没有调用它。
  2. 该库提供了一个清理函数,您确实调用了它,但它有错误并且存在泄漏。
  3. 库开发人员很懒,懒得提供清理功能。
  4. 您的应用程序异常结束并且未执行清理。

除了懒惰之外,有时清理工作也非常困难。例如,程序退出期间可能需要的用于异常处理的 IO 缓冲区和内存。

© www.soinside.com 2019 - 2024. All rights reserved.