[Libav-user] Libav memory leak and assert0 when remuxing AVPackets

Jeff Mueller jmueller at wi-tronix.com
Fri Mar 4 17:40:45 CET 2016


Hello libav users.

I'm a writing to hopefully expedite my task of eliminating a memory leak and assert0 crash when remuxing AVPackets.

I used the remuxing example at https://ffmpeg.org/doxygen/2.8/remuxing_8c-example.html as a reference for writing my code.

The remux example was a great help to me, but it only opens an input file, reads the AVPackets out, and writes these AVPackets to an output file and then exists.
My code opens an RTSP input URL, continuously reads H.264 AVPackets from the RTSP input, and writes these unmodified H.264 AVPackets out to an MP4 file with a fixed duration of 30 seconds.  This means after writing out 30 seconds worth of AVPackets, I close only the AVFormatContext for the MP4 output file and I leave the RTSP input open so I don't miss reading any frames from the camera.  I then open a new AVFormatContext for the next 30 second MP4 clip and continue writing out the H.264 AVPackets from the RTSP input.

Unfortunately, every time I open/close an MP4 output file, a memory leak occurs amounting to 250KB.  I don't know if this memory leak occurs in the remuxing example because the remuxing example code closes both the input and output and exits thereby eliminating any memory leaks when the OS terminates the process.

To get my code to run without crashing, I had to comment out a section of code that calls int attribute_align_arg avcodec_encode_video2(...) where it passes in NULL AVFrame's and then calls libffmpeg::av_interleaved_write_frame() to apparently flush any/all delayed AVPackets.  The AVPacket (for flushing) is initialized with .size = 0 and .data = NULL, before calling avcodec_encode_video2().

The crash (when running the flush code) occurs in avcodec_encode_video2() within libavcodec/utils.c  when the following assert0 is called:
av_assert0(avctx->codec->encode2);

Apparently the input AvCodecContext does not contain an encoding AvCodec.

The avctx and codec are created the same way as the remuxing example as follows:
data->VideoStream = libffmpeg::avformat_new_stream(data->FormatContext, pRtspInputAvCodecContext->codec);
              ret = avcodec_copy_context(data->VideoStream->codec, pRtspInputAvCodecContext);
              if (ret < 0)
              {
                     fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
              }
              else
              {
                     data->VideoStream->codec->codec_tag = 0;
                     if (data->FormatContext->oformat->flags & AVFMT_GLOBALHEADER)
                     {
                           data->VideoStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
                     }
              }

Does anyone know if flushing is required to eliminate the memory leak and if so, then how do I get flushing to work without running into the assert0 as described above?

Thank you,
Jeff Mueller
Senior Systems Engineer
[Beveled Wi-Tronix logo no shadow]
631 E Boughton Rd, Suite 240 | Bolingbrook, IL 60440-3455 | jmueller at wi-tronix.com<mailto:jmueller at wi-tronix.com> | Directions & Map<http://goo.gl/maps/WgZCJ>
[https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS9ii3VxAces63_UOaGvunLMLuIVzGCeDLCZaJyWXYeUPh18S7ESw] (630) 679-9927 Ext. xxx | [http://cpanelcloudvps.com/wp-content/uploads/2013/12/cell-phone-logo-pnghs-enterprises-9fttttbq.png]  (847) xxx-xxxx | Toll Free: (888) WI-TRONIX (948-7664) | [http://png-1.findicons.com/files/icons/1580/devine_icons_part_2/128/printer_and_fax.png]  (630) 679-9954

 [cid:image001.png at 01CEBDFF.75DF9460] <https://www.facebook.com/pages/Wi-Tronix/167958306554722>  [cid:image002.png at 01CEBDFF.75DF9460] <https://twitter.com/WiTronix>   [cid:image003.png at 01CEBDFF.75DF9460] <http://www.linkedin.com/company/wi-tronix-llc?trk=cp_followed_name_wi-tronix-llc>
CONFIDENTIALITY NOTICE
This email and any attached files are confidential, protected by copyright and may be legally privileged.  If you are not the intended addressee or have received the e-mail in error, any use of this e-mail or any copying, distribution or other dissemination of it is strictly prohibited. If you have received this transmission in error, please notify the sender immediately and then delete the e-mail. E-mail cannot be guaranteed to be secure, error free or free from viruses. Wi-Tronix, LLC and its affiliates  do not accept any liability whatsoever for any loss or damage which may be caused as a result of the transmission of this message by e-mail. If verification is required, please request a hard copy version.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 6708 bytes
Desc: image001.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 409 bytes
Desc: image002.jpg
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 576 bytes
Desc: image003.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 616 bytes
Desc: image004.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.png
Type: image/png
Size: 3320 bytes
Desc: image005.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.png
Type: image/png
Size: 3486 bytes
Desc: image006.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image007.png
Type: image/png
Size: 3431 bytes
Desc: image007.png
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160304/7ec168d2/attachment-0005.png>


More information about the Libav-user mailing list