[FFmpeg-trac] #7630(undetermined:new): Memory leak in avformat_

FFmpeg trac at avcodec.org
Wed Dec 19 19:02:03 EET 2018


#7630: Memory leak in avformat_
-------------------------------------+-------------------------------------
             Reporter:  Ace17        |                     Type:  defect
               Status:  new          |                 Priority:  normal
            Component:               |                  Version:
  undetermined                       |  unspecified
             Keywords:  valgrind     |               Blocked By:
  leak avformat_init_output          |  Reproduced by developer:  0
  av_write_trailer                   |
             Blocking:               |
Analyzed by developer:  0            |
-------------------------------------+-------------------------------------
 {{{
 #include <cassert>

 extern "C" {
 #include <libavformat/avformat.h> // AVOutputFormat
 #include <libavformat/avio.h> // avio_open2
 }

 auto const FILENAME = "hello.mp4";

 AVFormatContext* g_avformatCtx;

 void CreateAvContext() {
   g_avformatCtx = avformat_alloc_context();
   assert(g_avformatCtx);

   g_avformatCtx->oformat = av_guess_format("mpegts", nullptr, nullptr);
   assert(g_avformatCtx->oformat);
   assert(!(g_avformatCtx->oformat->flags & AVFMT_NOFILE));

   int ret = avio_open2(&g_avformatCtx->pb, FILENAME, AVIO_FLAG_WRITE,
 nullptr, nullptr);
   assert(ret >= 0);

   g_avformatCtx->url = av_strdup(FILENAME);
 }

 void DestroyAvContext() {
   //av_write_trailer(g_avformatCtx);
   avio_close(g_avformatCtx->pb);
   avformat_free_context(g_avformatCtx);
 }

 void AddStream() {
   auto const codec = avcodec_find_decoder_by_name("mpeg2video");
   assert(codec);

   auto stream = avformat_new_stream(g_avformatCtx, codec);
   assert(stream);

   auto codecpar = stream->codecpar;
   codecpar->codec_id = codec->id;
   codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
   codecpar->width = 128;
   codecpar->height = 128;
 }

 int main()
 {
   CreateAvContext();
   AddStream();

   int ret = avformat_write_header(g_avformatCtx, nullptr);
   assert(ret == 0);

   DestroyAvContext();
   return 0;
 }

 }}}

 How to reproduce:

 {{{
 g++ -g3 repro_libavformat_mux_leak.cpp `pkg-config libavformat libavcodec
 libavutil --cflags --libs`
 valgrind --leak-check=full ./a.out

 ==7541== 2,930 bytes in 1 blocks are definitely lost in loss record 243 of
 245
 ==7541==    at 0x4837EC3: memalign (in /usr/lib/x86_64-linux-gnu/valgrind
 /vgpreload_memcheck-amd64-linux.so)
 ==7541==    by 0x4837FF0: posix_memalign (in /usr/lib/x86_64-linux-
 gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==7541==    by 0x5FFC28A: av_malloc (in /usr/lib/x86_64-linux-
 gnu/libavutil.so.56.14.100)
 ==7541==    by 0x5FFC61C: av_mallocz (in /usr/lib/x86_64-linux-
 gnu/libavutil.so.56.14.100)
 ==7541==    by 0x498EE76: ??? (in /usr/lib/x86_64-linux-
 gnu/libavformat.so.58.12.100)
 ==7541==    by 0x4996F98: avformat_init_output (in /usr/lib/x86_64-linux-
 gnu/libavformat.so.58.12.100)
 ==7541==    by 0x4997754: avformat_write_header (in /usr/lib/x86_64-linux-
 gnu/libavformat.so.58.12.100)
 ==7541==    by 0x109409: main (repro_libavformat_mux_leak.cpp:54)
 ==7541==

 }}}

 Calling av_write_trailer makes the leak disappear. However, it's
 unconvenient in some situation where you want to quickly cancel the
 encoding/muxing, and discard the output file.

--
Ticket URL: <https://trac.ffmpeg.org/ticket/7630>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker


More information about the FFmpeg-trac mailing list