【问题标题】:How to correctly close and free ALSA resources如何正确关闭和释放 ALSA 资源
【发布时间】:2015-03-15 21:17:34
【问题描述】:

如何正确关闭和释放 ALSA(和硬件参数)资源? 我发现了很多例子。都不同。都有memleak。

例如:

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

int
main()
{
    snd_pcm_t *dev;

    snd_pcm_open(&dev, "default", SND_PCM_STREAM_PLAYBACK, 0);
    snd_pcm_close(dev);

    return 0;
}

Valgrind 报告:

==19586== LEAK SUMMARY:
==19586==    definitely lost: 0 bytes in 0 blocks
==19586==    indirectly lost: 0 bytes in 0 blocks
==19586==      possibly lost: 65,525 bytes in 2,020 blocks
==19586==    still reachable: 298 bytes in 6 blocks
==19586==         suppressed: 0 bytes in 0 blocks
==19586== Reachable blocks (those to which a pointer was found) are not shown.
==19586== To see them, rerun with: --leak-check=full --show-reachable=yes
==19586== 
==19586== ERROR SUMMARY: 116 errors from 116 contexts (suppressed: 4 from 4)
--19586-- 
--19586-- used_suppression:      2 dl-hack3-cond-1
--19586-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a
==19586== 
==19586== ERROR SUMMARY: 116 errors from 116 contexts (suppressed: 4 from 4)

更新:

没有 snd_pcm_close() 我们有来自 117 个上下文的 117 个错误)))

【问题讨论】:

标签: c valgrind alsa


【解决方案1】:

用 alsalib 1.1.4.1 测试了这个例子:

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

int main()
{
    snd_pcm_t *dev;

    snd_pcm_open(&dev, "default", SND_PCM_STREAM_PLAYBACK, 0);
    snd_pcm_close(dev);
    snd_config_update_free_global();

    return 0;
}

按照 Paolo 的建议添加 snd_config_update_free_global(),valgrind 会显示很多这样的警告:

4,320 bytes in 60 blocks are possibly lost in loss record 91 of 94

摘要显示可能丢失的块:

==499== LEAK SUMMARY:
==499==    definitely lost: 0 bytes in 0 blocks
==499==    indirectly lost: 0 bytes in 0 blocks
==499==      possibly lost: 43,011 bytes in 1,311 blocks
==499==    still reachable: 111,131 bytes in 128 blocks
==499==         suppressed: 0 bytes in 0 blocks
==499== Reachable blocks (those to which a pointer was found) are not shown.
==499== To see them, rerun with: --leak-check=full --show-leak-kinds=all

可以用它做什么?

【讨论】:

    【解决方案2】:

    如果您在 snd_pcm_close(handle) 之后调用 snd_config_update_free_global() 释放全局配置,您将不会获得泄漏;

    【讨论】:

    • 谢谢。但这是 Debian 6 上 alsa 的错误。今天不实际 - 旧的旧马厩不见了 :)
    猜你喜欢
    • 2016-02-03
    • 2013-04-02
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多