linux - alsa - 内存泄漏?

标签 linux memory-leaks valgrind alsa

我一直在追踪内存泄漏(由“valgrind --leak-check=yes”报告),它似乎来自 ALSA。这段代码已经在自由世界中发布了一段时间,所以我猜这是我做错了什么。

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

int main (int argc, char *argv[])
{
    snd_ctl_t *handle;

    int err = snd_ctl_open( &handle, "hw:1", 0 );
    printf( "snd_ctl_open: %d\n", err );

    err = snd_ctl_close(handle);
    printf( "snd_ctl_close: %d\n", err );
}

输出看起来像这样:

[root@aeolus alsa]# valgrind --leak-check=yes ./test2
==16296== Memcheck, a memory error detector
==16296== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==16296== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==16296== Command: ./test2
==16296==
snd_ctl_open: 0
snd_ctl_close: 0
==16296==
==16296== HEAP SUMMARY:
==16296==     in use at exit: 22,912 bytes in 1,222 blocks
==16296==   total heap usage: 1,507 allocs, 285 frees, 26,236 bytes allocated
==16296==
==16296== 4 bytes in 2 blocks are possibly lost in loss record 1 of 62
==16296==    at 0x4007100: malloc (vg_replace_malloc.c:270)
==16296==    by 0x340F7F: strdup (in /lib/libc-2.5.so)
==16296==    by 0x624C6B5: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624CA5B: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624CD81: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624F311: snd_config_update_r (in /lib/libasound.so.2.0.0)
==16296==    by 0x624FAD7: snd_config_update (in /lib/libasound.so.2.0.0)
==16296==    by 0x625DA22: snd_ctl_open (in /lib/libasound.so.2.0.0)
==16296==    by 0x804852F: main (test2.cpp:9)

并继续一些页面以

==16296== 2,052 bytes in 57 blocks are possibly lost in loss record 62 of 62
==16296==    at 0x4005EB4: calloc (vg_replace_malloc.c:593)
==16296==    by 0x624A268: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624A38F: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624CA33: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624CCC9: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624CD81: ??? (in /lib/libasound.so.2.0.0)
==16296==    by 0x624F311: snd_config_update_r (in /lib/libasound.so.2.0.0)
==16296==    by 0x624FAD7: snd_config_update (in /lib/libasound.so.2.0.0)
==16296==    by 0x625DA22: snd_ctl_open (in /lib/libasound.so.2.0.0)
==16296==    by 0x804852F: main (test2.cpp:9)
==16296==
==16296== LEAK SUMMARY:
==16296==    definitely lost: 0 bytes in 0 blocks
==16296==    indirectly lost: 0 bytes in 0 blocks
==16296==      possibly lost: 22,748 bytes in 1,216 blocks
==16296==    still reachable: 164 bytes in 6 blocks
==16296==         suppressed: 0 bytes in 0 blocks
==16296== Reachable blocks (those to which a pointer was found) are not shown.
==16296== To see them, rerun with: --leak-check=full --show-reachable=yes
==16296==
==16296== For counts of detected and suppressed errors, rerun with: -v
==16296== ERROR SUMMARY: 56 errors from 56 contexts (suppressed: 19 from 8)

这是因为我在一个项目中使用 ALSA 并开始看到这个巨大的泄漏……或者至少是上述泄漏的报告。

所以问题是:是我、ALSA 还是 valgrind 在这里遇到了问题?

最佳答案

http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=MEMORY-LEAK;hb=HEAD说:

                          Memory leaks - really?
                          ----------------------

Note that some developers are thinking that the ALSA library has some memory leaks. Sure, it can be truth, but before contacting us, please, be sure that these leaks are not forced.

The biggest reported leak is that the global configuration is cached for next usage. If you do not want this feature, simply, call snd_config_update_free_global() after all snd_*_open*() calls. This function will free the cache.

关于linux - alsa - 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478861/

相关文章:

linux - 如何在 Red Hat 3.4.6-3 中安装 yum

python - 什么是 os.fdopen() 语义?

multithreading - 关于 Goroutines 的 Golang 内存泄漏

c++ - valgrind 和 std::unique_ptr --- 误报与否?

C - Mac 上的 Valgrind 显示与 linux 不同的结果?

c - 出现段错误。不知道为什么

linux - Bash 中的 Xargs 并行性

python - 产生一个进程是处理 python 内存泄漏的好方法吗?

窗口/选项卡之间的 javascript/jquery 内存空间

c - 尝试添加到 C 中链接列表末尾时出现段错误