[Libav-user] Memory Leaks?

wm4 nfxjfg at googlemail.com
Sun Apr 6 00:44:44 CEST 2014


On Sun, 6 Apr 2014 00:06:12 +0200
Harald Schlangmann <harry at gps-laptimer.de> wrote:

> I’m unreferencing it when a frame has been completely read:
> 
> >>                int     frameFinished;
> >> 
> >>                ret = avcodec_decode_video2 (videoSource->pVideoCodecCtx, videoSource->pVideoFrameRaw, &frameFinished, &packet);
> >> 
> >>                if (ret>=0)
> >>                {
> >>                    if (frameFinished)
> >>                    {
> >> 			…
> >> 
> >>                        av_frame_unref (videoSource->pVideoFrameRaw); 
> 
> Unreferencing it before the avcodec_decode_video2 is most probably wrong (at least in the theoretical case the frame comes in more than 1 package)?

No, the decoder will keep its own references to frames it needs. But at
least in some versions of ffmpeg, you had to pass a "blank" 
(unreferenced) frame to the decoder.

Also, calling av_frame_unref() is probably wrong if you haven't enabled
reference counting.

IMO the safe way to do this is:

- require at least ffmpeg 2.1.x
- set AVCodecContext.refcounted_frames=1 before avcodec_open2()
- create a single AVFrame with av_frame_alloc()
- call av_frame_unref() before passing it to the decoder
  (you can reuse the AVFrame, as long as you unref it)
- on program termination call av_frame_free()

Everything else is slightly or completely broken or works only on older
FFmpeg versions. It's possible that you don't need to unref the
frame on very new FFmpeg, but I haven't checked.


More information about the Libav-user mailing list