[Libav-user] [libav-user] Memory leak in Mac OS X Lion

Michael Bradshaw mbradshaw at sorensonmedia.com
Tue Oct 16 15:56:22 CEST 2012


On Oct 16, 2012 2:11 AM, "Quy Pham Sy" <phamsyquybk at gmail.com> wrote:
>
> Hi,
>
> I made a simple console test base on Dranger 's tutorial code by
> replace deprecated function by the new versions.
> here the full source code:
>
>
https://github.com/phamquy/FFmpeg-tutorial-samples/blob/master/tutorial01.c
>
> It works fine, but there is a huge memory leak (i use Instrument to
> for leak detection).
> It seem like the leaks caused by the while loop,
> there is no leak after i commented out this part:
>
>
>     i=0;
>     while(av_read_frame(pFormatCtx, &packet)>=0) {
>           if(packet.stream_index==videoStreamIdx) {
>             avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);
>
>             if(frameFinished) {
>                 static struct SwsContext *img_convert_ctx;
>                 int w = pCodecCtx->width;
>                 int h = pCodecCtx->height;
>                 img_convert_ctx = sws_getContext(w, h, pCodecCtx->pix_fmt,
>                                                  w, h, PIX_FMT_RGB24,
>                                                  SWS_BICUBIC, NULL, NULL,
NULL);
>
>                 sws_scale(img_convert_ctx, (const uint8_t * const
> *)pFrame->data,
>                           pFrame->linesize, 0, pCodecCtx->height,
>                           pFrameRGB->data, pFrameRGB->linesize);
>
>                 if(++i<=5)
>                     SaveFrame(pFrameRGB, pCodecCtx->width,
pCodecCtx->height,
>                               i);
>             }
>         }
>         av_free_packet(&packet);
>     }
>
>
> What is wrong with this code?

One thing is that you never release the SwsContext. It's best to just get
it once and re-use it, and then release it at the end. It's also possible
you're leaking something in SaveFrame.

--Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121016/6c562bd1/attachment.html>


More information about the Libav-user mailing list