[Libav-user] Basic problem : memory leak when playing several movies
wm4
nfxjfg at googlemail.com
Fri Sep 13 12:47:43 CEST 2013
On Thu, 12 Sep 2013 14:46:31 +0200
Fulbert Boussaton <42 at flubb.net> wrote:
> AVFormatContext* Ctx;
> avformat_open_input(&Ctx, <...>, NULL, &Options);
> avformat_find_stream_info(Ctx, NULL);
> AVCodecContext* VCodecCtx = Ctx->streams[0]->codec;
> AVCodec* TargetVideoCodec = avcodec_find_decoder(VCodecCtx->codec_id);
> avcodec_open2(VCodecCtx, TargetVideoCodec, NULL);
> AVFrame* TargetFrame = avcodec_alloc_frame();
> AVFrame* TargetFrameRGB = avcodec_alloc_frame();
> int FrameSizeInBytes = avpicture_get_size(PIX_FMT_RGB24, VCodecCtx->width, VCodecCtx->height);
> uint8_t* FrameBuffer = (uint8_t*) av_malloc(FrameSizeInBytes);
> avpicture_fill((AVPicture*) TargetFrameRGB, FrameBuffer, PIX_FMT_RGB24, VCodecCtx->width, VCodecCtx->height);
> struct SwsContext* ScaleCtx = sws_getContext(VCodecCtx->width, VCodecCtx->height, VCodecCtx->pix_fmt, VCodecCtx->width, VCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
>
> And here are the functions I call to release all the previous structures :
>
> sws_freeContext(ScaleCtx);
> avpicture_free((AVPicture*)TargetFrameRGB);
> av_free(FrameBuffer);
> avcodec_free_frame(&TargetFrame);
> avcodec_free_frame(&TargetFrameRGB);
> avcodec_close(VCodecCtx);
> avformat_close_input(&Ctx);
This all looks very wrong. I'd just stick to the AVFrame API, and not
touch AVPicture. In some cases, you cast AVFrame to AVPicture, which
only works by coincidence. (I'm not sure if it's possibly supposed to
work - as far as I'm concerned AVPicture is ridiculous. You can get
everything done without using AVPicture at all.)
See libavutil/frame.h.
More information about the Libav-user
mailing list