Opened 3 years ago

Last modified 17 months ago

#2937 new defect

avformat_new_stream() memory leak

Reported by: jlsantiago0 Owned by:
Priority: normal Component: avformat
Version: git-master Keywords: leak
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

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);


Attachments (1)

ffmpeg-muxer-memory-leak.patch (673 bytes) - added by jlsantiago0 3 years ago.
Patch to fix the muxer memory leak.

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by jlsantiago0

Patch to fix the muxer memory leak.

comment:1 Changed 3 years ago by cehoyos

  • Keywords memory removed
  • Priority changed from important to normal

Please send patches to the ffmpeg-devel mailing list where they will be reviewed.
(If you believe that this is not clearly explained on the New Ticket page, please tell us!)

comment:2 Changed 3 years ago by jlsantiago0

OK. I sent the patch and the description to the ffmpeg-devel list.

Your mail to 'ffmpeg-devel' with the subject

    avformat_new_stream memory leak

Is being held until the list moderator can review it for approval.

The reason it is being held:

    Post by non-member to a members-only list

Either the message will get posted to the list, or you will receive
notification of the moderator's decision.  If you would like to cancel
this posting, please visit the following URL:

    http://ffmpeg.org/mailman/confirm/ffmpeg-devel/da26e20d09b6e38059c4f81267ee9bd0fa04fb4e
Last edited 3 years ago by jlsantiago0 (previous) (diff)

comment:4 Changed 17 months ago by jlsantiago0

This issue is resolved and can be closed as fixed.

Note: See TracTickets for help on using tickets.