2015-01-17 53 views
0

如何正確關閉並釋放ALSA(和hw params)資源? 我發現了很多例子。所有不同。都有memleak。如何正確關閉並釋放ALSA資源

例如:

#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) 

UPD:

沒有snd_pcm_close()我們有來自117點的上下文117個錯誤)))

+0

[alsa - mem leak?](http://stackoverflow.com/questions/13478861/alsa-mem-leak) –

+0

@CL可能重複。哪裏繼續討論?這裏?因爲仍然存在memleak(來自2個上下文的2個錯誤) – Deep

+0

這些情況是什麼? –

回答

0

你贏了如果通過調用snd_config_update_free_global()AF釋放全局配置,則不會獲得泄漏TER snd_pcm_close(句柄);

+0

謝謝。但是這是Debian 6上的alsa錯誤。今天不是真正的 - 舊的stable stable不見了:) – Deep

0

測試該實施例中與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; 
} 

添加snd_config_update_free_global()由Paolo所建議的,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 

該怎麼辦?