[FFmpeg-trac] #7251(avcodec:new): Memory/Resource Leak in avcodec_open2() on failure
FFmpeg
trac at avcodec.org
Fri Jun 8 22:03:59 EEST 2018
#7251: Memory/Resource Leak in avcodec_open2() on failure
-------------------------------------+-------------------------------------
Reporter: jlsantiago0 | Type: defect
Status: new | Priority: normal
Component: avcodec | Version: git-
Keywords: memory | master
leak, resource leak | Blocked By:
Blocking: | Reproduced by developer: 0
Analyzed by developer: 0 |
-------------------------------------+-------------------------------------
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
avcodec_free_context(&avctx);
}
}}}
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.
--
Ticket URL: <https://trac.ffmpeg.org/ticket/7251>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker
More information about the FFmpeg-trac
mailing list