[Libav-user] How can I free data buffers missed by avcodec_free_frame?

Alex Cohn alexcohn at netvision.net.il
Sat Mar 30 22:15:56 CET 2013

On Fri, Mar 29, 2013 at 9:27 PM, Czarnek, Matt <mczarnek at objectvideo.com> wrote:
> In the description for avcodec_free_frame, it states:
> "Warning: this function does NOT free the data buffers themselves"
> I have allocated my buffers as such:
> int curAVFramesize = avpicture_get_size(PIX_FMT_YUV420P, ccontext->width,
> ccontext->height);
> uint8_t* curAVFramePicBuffer = (uint8_t*)(av_malloc(curAVFramesize));
> AVFrame *curAVFrame=avcodec_alloc_frame();
> avpicture_fill((AVPicture *)curAVFrame,curAVFramePicBuffer,
> PIX_FMT_YUV420P,ccontext->width, ccontext->height);
> I figured that the warning meant calling 'avpicture_free' was nessecary. So
> I've been freeing it as:
> avpicture_free((AVPicture *)curAVFrame);
> avcodec_free_frame((AVFrame **)(&curAVFrame));

See http://ffmpeg.org/doxygen/trunk/group__lavc__picture.html:

void 	avpicture_free (AVPicture *picture):
 	Free a picture previously allocated by avpicture_alloc().

So, you can either switch to use avpicture_alloc(), or get the pic
buffer from curAVFrame and use av_free() to free it.

> Usually my program doesn't complain but every once in a while, after calling
> 'avpicture_free' but before 'avcodec_free_frame' it'll throw a heap
> allocation error.
> Here is the entire function: http://pastebin.com/jHecUySU
> Is avpicture_free needed?  Any thoughts as to what might be happening?

Sometimes, the result of your allocations is not similar to what
libavcodec does in avpicture_alloc().

Alex Cohn

More information about the Libav-user mailing list