[FFmpeg-devel] avformat_new_stream memory leak
Jose Santiago
jsantiago at fastmail.us
Tue Sep 3 23:45:46 CEST 2013
I created a ticket for this issue
https://ffmpeg.org/trac/ffmpeg/ticket/2937 and they asked me to send
this to the ffmpeg-devel list. I have attached a patch to fix this issue.
Here is the original issue description:
avformat_new_stream() calls avcodec_alloc_context3() which can allocate
data that is not free'd by the manual freeing of the codec context done
by ff_free_stream() which is called by avformat_free_context(). In
particular the st->codec->priv_data is allocated, but not free'd. The
attached patch modifies ff_free_stream() to call avcodec_close() to
properly close the codec context allocated in avformat_new_stream() .
For instance in the following code:
AVFormatContext * formatCtx;
AVCodec * codec;
AVStream * avStream;
AVOutputFormat * outputFormat;
const char * shortName = "mpegts";
const char * outputURL = "file://somefile.ts";
outputFormat = av_guess_format(shortName, outputURL, NULL);
assert(outputFormat != NULL);
avformat_alloc_output_context2(&formatCtx, outputFormat, shortName, outputURL);
assert(formatCtx != NULL);
avio_open2(&formatCtx->pb, outputURL, AVIO_FLAG_WRITE, NULL, NULL);
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
assert(codec != NULL);
avStream = avformat_new_stream(formatCtx, codec);
assert(avStream != NULL);
avformat_write_header(formatCtx, NULL);
av_write_trailer(formatCtx);
avio_close(formatCtx->pb);
formatCtx->pb = NULL;
# Not all of the st[]->codec is free'd here.
avformat_free_context(formatCtx);
-------------- next part --------------
diff -Naur master-20130903.orig/libavformat/utils.c master-20130903/libavformat/utils.c
--- master-20130903.orig/libavformat/utils.c 2013-09-03 13:33:49.000000000 -0500
+++ master-20130903/libavformat/utils.c 2013-09-03 14:26:42.000000000 -0500
@@ -3201,9 +3201,10 @@
av_dict_free(&st->metadata);
av_freep(&st->probe_data.buf);
av_freep(&st->index_entries);
- av_freep(&st->codec->extradata);
- av_freep(&st->codec->subtitle_header);
- av_freep(&st->codec);
+ if (st->codec) {
+ avcodec_close(st->codec);
+ av_freep(&st->codec);
+ }
av_freep(&st->priv_data);
if (st->info)
av_freep(&st->info->duration_error);
More information about the ffmpeg-devel
mailing list