Opened 11 days ago

Last modified 8 days ago

#7251 new defect

Memory/Resource Leak in avcodec_open2() on failure

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


When calling avcodec_open2(), memory and other resources can leak if the encoder init fails or other steps within the codec open function fails. This includes memory and threads that can be created, but not destroyed in the case of a failure.

For instance, if you attempt to intialize an MPEG2 Video Encoder and enable slice based threaded encoding. If you do not set the resolution, fps, or the pixel format to a valid value. The encoder inititialization will fail, indicating that the codec context parameter is invalid for the selected encoder. It properly returns an error. But it does not destroy the threads it created. Nor does it destory all of the memory that the encoder init allocated before returning the failure.

For instance you can run valgrind with the following program to demonstrate the problem:

#include <libavcodec/avcodec.h>
#include <libavutil/avassert.h>

int main(void)
   AVCodec * codec;
   AVCodecContext * avctx;

   codec = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);
   av_assert0(codec != NULL);
   avctx = avcodec_alloc_context3(codec);
   av_assert0(avctx != NULL);

   // Basic configuration
   // You can get the results by setting the pix_fmt
   // and/or resolution to some invalid or unsupported value.
   avctx->pix_fmt = AV_PIX_FMT_YUV420P;
   avctx->width = 640;
   avctx->height = 480;
   // Set some ffps that is not supported by MPEG2 Video
   avctx->time_base.num = 91;
   avctx->time_base.den = 2201;
   avctx->ticks_per_frame = 1;
   // Enable slice thread encoding
   avctx->thread_count = 10;
   avctx->thread_type = 2;
   avctx->active_thread_type = 2;

   // Open the Codec for encoding
   avcodec_open2(avctx, codec, NULL);

   // close and free the Codec

NOTE: If you put contents of main() in a loop to run forever, it will eventually make your system unstable as the number of threads created and not destroyed by the program exceed the number of supported threads in the system.

Attachments (4)

valgrind.txt (76.6 KB) - added by jlsantiago0 11 days ago.
Valgrind output
ffmpeg-ticket-7251.c (818 bytes) - added by jlsantiago0 11 days ago.
Program that demonstrates the problem
ffmpeg-issue-7251.patch (2.3 KB) - added by jlsantiago0 11 days ago.
Patch that resolves the issue
ffmpeg-issue-7251.2.patch (2.3 KB) - added by jlsantiago0 8 days ago.
Updated patch fixing style issues

Download all attachments as: .zip

Change History (9)

Changed 11 days ago by jlsantiago0

Valgrind output

Changed 11 days ago by jlsantiago0

Program that demonstrates the problem

Changed 11 days ago by jlsantiago0

Patch that resolves the issue

comment:1 Changed 11 days ago by jamrial

Please, send your patches to the ffmpeg-devel mailing list where they can be properly reviewed.

Also, AVCodecContext->active_thread_type is not a field that should be set by the API user, as stated in the doxy.

comment:2 Changed 10 days ago by cehoyos

  • Keywords mpeg2video added; memory resource removed

To simplify the reviewing process please use the same bracing style as the rest of the source file (visible twice in your patch file).

Changed 8 days ago by jlsantiago0

Updated patch fixing style issues

comment:3 Changed 8 days ago by jlsantiago0

Modified the patch, fixing the brace style and submitted it to the libav-user mailing list.

comment:4 Changed 8 days ago by cehoyos

Send the patch to the development mailing list instead, no review can be done on the libav-user mailing list:

comment:5 Changed 8 days ago by cehoyos

Please provide a patch made with git format-patch, nothing else is accepted, sorry for not realizing this earlier!

Note: See TracTickets for help on using tickets.