[FFmpeg-devel] [PATCH 3/3] avformat/segment: Use deinit function to deinitialize muxer

sebechlebskyjan at gmail.com sebechlebskyjan at gmail.com
Mon Jun 20 17:24:07 CEST 2016


From: Jan Sebechlebsky <sebechlebskyjan at gmail.com>

This commit moves all deinitialization of SegmentContext to
seg_free_context function and registers this function as
.deinit function of segment muxer. This also fixes memory leaks
caused by context not being deinitialized when write_header
call of segment muxer fails.

Signed-off-by: Jan Sebechlebsky <sebechlebskyjan at gmail.com>
---
 libavformat/segment.c | 49 +++++++++++++++++++++++--------------------------
 1 file changed, 23 insertions(+), 26 deletions(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index df37a56..872233b 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -627,9 +627,28 @@ static int select_reference_stream(AVFormatContext *s)
     return 0;
 }
 
-static void seg_free_context(SegmentContext *seg)
+static void seg_free_context(AVFormatContext *s)
 {
-    ff_format_io_close(seg->avf, &seg->list_pb);
+    SegmentContext *seg = s->priv_data;
+    SegmentListEntry *cur, *next;
+
+    if (seg->list)
+        ff_format_io_close(s, &seg->list_pb);
+
+    av_dict_free(&seg->format_options);
+    av_opt_free(seg);
+    av_freep(&seg->times);
+    av_freep(&seg->frames);
+    av_freep(&seg->cur_entry.filename);
+
+    cur = seg->segment_list_entries;
+    while (cur) {
+        next = cur->next;
+        av_freep(&cur->filename);
+        av_free(cur);
+        cur = next;
+    }
+
     avformat_free_context(seg->avf);
     seg->avf = NULL;
 }
@@ -801,8 +820,6 @@ static int seg_init(AVFormatContext *s)
 
 fail:
     av_dict_free(&options);
-    if (ret < 0)
-        seg_free_context(seg);
 
     return ret;
 }
@@ -917,9 +934,6 @@ fail:
         seg->segment_frame_count++;
     }
 
-    if (ret < 0)
-        seg_free_context(seg);
-
     return ret;
 }
 
@@ -944,25 +958,6 @@ static int seg_write_trailer(struct AVFormatContext *s)
         ret = segment_end(s, 1, 1);
     }
 fail:
-    if (seg->list)
-        ff_format_io_close(s, &seg->list_pb);
-
-    av_dict_free(&seg->format_options);
-    av_opt_free(seg);
-    av_freep(&seg->times);
-    av_freep(&seg->frames);
-    av_freep(&seg->cur_entry.filename);
-
-    cur = seg->segment_list_entries;
-    while (cur) {
-        next = cur->next;
-        av_freep(&cur->filename);
-        av_free(cur);
-        cur = next;
-    }
-
-    avformat_free_context(oc);
-    seg->avf = NULL;
     return ret;
 }
 
@@ -1045,6 +1040,7 @@ AVOutputFormat ff_segment_muxer = {
     .write_packet   = seg_write_packet,
     .write_trailer  = seg_write_trailer,
     .check_bitstream = seg_check_bitstream,
+    .deinit = seg_free_context,
     .priv_class     = &seg_class,
 };
 
@@ -1064,5 +1060,6 @@ AVOutputFormat ff_stream_segment_muxer = {
     .write_packet   = seg_write_packet,
     .write_trailer  = seg_write_trailer,
     .check_bitstream = seg_check_bitstream,
+    .deinit = seg_free_context,
     .priv_class     = &sseg_class,
 };
-- 
1.9.1



More information about the ffmpeg-devel mailing list