[FFmpeg-cvslog] lavd/xv: free resources on errors

Lukasz Marek git at videolan.org
Fri Nov 15 10:00:24 CET 2013


ffmpeg | branch: master | Lukasz Marek <lukasz.m.luki at gmail.com> | Wed Nov 13 23:40:47 2013 +0100| [995f450b44be02cc159ac825f038309e7a3de51e] | committer: Stefano Sabatini

lavd/xv: free resources on errors

xv_write_header callback leave not freed resources on errors.

Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
Signed-off-by: Stefano Sabatini <stefasab at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=995f450b44be02cc159ac825f038309e7a3de51e
---

 libavdevice/xv.c |   53 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/libavdevice/xv.c b/libavdevice/xv.c
index 4492df0..ad60482 100644
--- a/libavdevice/xv.c
+++ b/libavdevice/xv.c
@@ -81,13 +81,28 @@ static int xv_get_tag_from_format(enum AVPixelFormat format)
     return 0;
 }
 
+static int xv_write_trailer(AVFormatContext *s)
+{
+    XVContext *xv = s->priv_data;
+    if (xv->display) {
+        XShmDetach(xv->display, &xv->yuv_shminfo);
+        if (xv->yuv_image)
+            shmdt(xv->yuv_image->data);
+        XFree(xv->yuv_image);
+        if (xv->gc)
+            XFreeGC(xv->display, xv->gc);
+        XCloseDisplay(xv->display);
+    }
+    return 0;
+}
+
 static int xv_write_header(AVFormatContext *s)
 {
     XVContext *xv = s->priv_data;
     unsigned int num_adaptors;
     XvAdaptorInfo *ai;
     XvImageFormatValues *fv;
-    int num_formats = 0, j, tag;
+    int num_formats = 0, j, tag, ret;
     AVCodecContext *encctx = s->streams[0]->codec;
 
     if (   s->nb_streams > 1
@@ -122,20 +137,26 @@ static int xv_write_header(AVFormatContext *s)
                                      xv->window_width, xv->window_height,
                                      0, 0, 0);
     if (!xv->window_title) {
-        if (!(xv->window_title = av_strdup(s->filename)))
-            return AVERROR(ENOMEM);
+        if (!(xv->window_title = av_strdup(s->filename))) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
     }
     XStoreName(xv->display, xv->window, xv->window_title);
     XMapWindow(xv->display, xv->window);
 
-    if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success)
-        return AVERROR_EXTERNAL;
+    if (XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &num_adaptors, &ai) != Success) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
     xv->xv_port = ai[0].base_id;
     XvFreeAdaptorInfo(ai);
 
     fv = XvListImageFormats(xv->display, xv->xv_port, &num_formats);
-    if (!fv)
-        return AVERROR_EXTERNAL;
+    if (!fv) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
     for (j = 0; j < num_formats; j++) {
         if (fv[j].id == tag) {
             break;
@@ -147,7 +168,8 @@ static int xv_write_header(AVFormatContext *s)
         av_log(s, AV_LOG_ERROR,
                "Device does not support pixel format %s, aborting\n",
                av_get_pix_fmt_name(encctx->pix_fmt));
-        return AVERROR(EINVAL);
+        ret = AVERROR(EINVAL);
+        goto fail;
     }
 
     xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
@@ -166,6 +188,9 @@ static int xv_write_header(AVFormatContext *s)
     shmctl(xv->yuv_shminfo.shmid, IPC_RMID, 0);
 
     return 0;
+  fail:
+    xv_write_trailer(s);
+    return ret;
 }
 
 static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
@@ -195,18 +220,6 @@ static int xv_write_packet(AVFormatContext *s, AVPacket *pkt)
     return 0;
 }
 
-static int xv_write_trailer(AVFormatContext *s)
-{
-    XVContext *xv = s->priv_data;
-
-    XShmDetach(xv->display, &xv->yuv_shminfo);
-    shmdt(xv->yuv_image->data);
-    XFree(xv->yuv_image);
-    XFreeGC(xv->display, xv->gc);
-    XCloseDisplay(xv->display);
-    return 0;
-}
-
 #define OFFSET(x) offsetof(XVContext, x)
 static const AVOption options[] = {
     { "display_name", "set display name",       OFFSET(display_name), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },



More information about the ffmpeg-cvslog mailing list