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