[FFmpeg-devel] [PATCH]Fix memleak if decoder init fails

Carl Eugen Hoyos cehoyos at ag.or.at
Sat Mar 2 22:02:39 CET 2013


Hi!

Both attached patches fix the memleak that happens if the decoder 
initialisation fails, see ticket #1244 for an example.

Please review, Carl Eugen
-------------- next part --------------
diff --git a/ffmpeg.c b/ffmpeg.c
index 5bc98a7..779b49e 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2437,8 +2437,13 @@ static int transcode_init(void)
 
     /* init input streams */
     for (i = 0; i < nb_input_streams; i++)
-        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
+        if ((ret = init_input_stream(i, error, sizeof(error))) < 0) {
+            for (i = 0; i < nb_output_streams; i++) {
+                ost = output_streams[i];
+                avcodec_close(ost->st->codec);
+            }
             goto dump_format;
+        }
 
     /* discard unused programs */
     for (i = 0; i < nb_input_files; i++) {
-------------- next part --------------
diff --git a/ffmpeg.c b/ffmpeg.c
index 5bc98a7..1352357 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2390,6 +2390,11 @@ static int transcode_init(void)
         }
     }
 
+    /* init input streams */
+    for (i = 0; i < nb_input_streams; i++)
+        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
+            goto dump_format;
+
     /* open each encoder */
     for (i = 0; i < nb_output_streams; i++) {
         ost = output_streams[i];
@@ -2435,11 +2440,6 @@ static int transcode_init(void)
         }
     }
 
-    /* init input streams */
-    for (i = 0; i < nb_input_streams; i++)
-        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
-            goto dump_format;
-
     /* discard unused programs */
     for (i = 0; i < nb_input_files; i++) {
         InputFile *ifile = input_files[i];


More information about the ffmpeg-devel mailing list