[FFmpeg-devel] lavd/alsa: fixes ALSA resource leak on closing the PCM handle

Submitted by Takayuki 'January June' Suwa on May 8, 2017, 4:09 a.m.

Details

Message ID 1494216594-1930-1-git-send-email-jjsuwa.sys3175@gmail.com
State New
Headers show

Commit Message

Takayuki 'January June' Suwa May 8, 2017, 4:09 a.m.
By avoiding thread race condition (CAS-ing with null handle)

---
 libavdevice/alsa.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Comments

James Almer May 8, 2017, 4:14 a.m.
On 5/8/2017 1:09 AM, Takayuki 'January June' Suwa wrote:
> By avoiding thread race condition (CAS-ing with null handle)
> 
> ---
>  libavdevice/alsa.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/libavdevice/alsa.c b/libavdevice/alsa.c
> index 1bbff30..81187ab 100644
> --- a/libavdevice/alsa.c
> +++ b/libavdevice/alsa.c
> @@ -30,6 +30,7 @@
>  
>  #include <alsa/asoundlib.h>
>  #include "avdevice.h"
> +#include "libavutil/atomic.h"
>  #include "libavutil/avassert.h"
>  #include "libavutil/channel_layout.h"
>  
> @@ -299,13 +300,14 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
>  av_cold int ff_alsa_close(AVFormatContext *s1)
>  {
>      AlsaData *s = s1->priv_data;
> +    snd_pcm_t *h = avpriv_atomic_ptr_cas((void* volatile *)&s->h, s->h, 0);

The libavutil/atomics wrappers are going to be removed soon once all
code is finally ported. You should instead use C11 atomics for this.

>  
> -    snd_pcm_nonblock(s->h, 0);
> -    snd_pcm_drain(s->h);
> +    snd_pcm_nonblock(h, 0);
> +    snd_pcm_drain(h);
>      av_freep(&s->reorder_buf);
>      if (CONFIG_ALSA_INDEV)
>          ff_timefilter_destroy(s->timefilter);
> -    snd_pcm_close(s->h);
> +    snd_pcm_close(h);
>      return 0;
>  }
>  
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

Patch hide | download patch | download mbox

diff --git a/libavdevice/alsa.c b/libavdevice/alsa.c
index 1bbff30..81187ab 100644
--- a/libavdevice/alsa.c
+++ b/libavdevice/alsa.c
@@ -30,6 +30,7 @@ 
 
 #include <alsa/asoundlib.h>
 #include "avdevice.h"
+#include "libavutil/atomic.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 
@@ -299,13 +300,14 @@  av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
 av_cold int ff_alsa_close(AVFormatContext *s1)
 {
     AlsaData *s = s1->priv_data;
+    snd_pcm_t *h = avpriv_atomic_ptr_cas((void* volatile *)&s->h, s->h, 0);
 
-    snd_pcm_nonblock(s->h, 0);
-    snd_pcm_drain(s->h);
+    snd_pcm_nonblock(h, 0);
+    snd_pcm_drain(h);
     av_freep(&s->reorder_buf);
     if (CONFIG_ALSA_INDEV)
         ff_timefilter_destroy(s->timefilter);
-    snd_pcm_close(s->h);
+    snd_pcm_close(h);
     return 0;
 }