我有一个用libmpg123和libao播放mp3文件的C程序。我使用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
可能发生泄漏,这是否意味着我无法从我的Angular 修复它?
如果你想知道,在日志中显示的play_sound()
函数可以在这里找到。
2条答案
按热度按时间ct2axkht1#
您可以找到这些库的源代码(如果可用),打开调试信息(-g)在本地编译它们,然后链接到您的项目。现在Valgrind可以告诉你这些库的漏洞在哪里,即你会看到实际的堆栈,而不是很多“??"。如果问题不在您这边(例如,您忘记从您应该拥有的库中释放内存),那么您可以自己修复错误并将修复程序提供给库的作者,或者告诉他们问题并要求他们修复它。
l7mqbcuq2#
有几个可能的原因
1.库提供了一个清理函数,但您没有调用它。
1.这个库提供了一个清理函数,你确实调用了它,但它有错误和泄漏。
1.库开发人员很懒惰,不愿意提供清理功能。
1.您的应用程序异常结束,并且未执行清理。
除了懒惰,还有一些情况下,清理是非常困难的。例如,在程序退出期间可能需要的用于异常处理的IO缓冲区和存储器。