[FFmpeg-devel] Memory leak: av_interleaved_write_frame

Pavel Pavlov pavel
Thu Jun 24 21:35:29 CEST 2010


Not sure what is wrong and not sure if anybody cares. I started to get lots of leaks from av_interleaved_write_frame and filled out a bug report with a possible fix that I did to avoid the leaks. It appears that current avformat still has the problem.
Here's the reported problem (sorry for typos) https://roundup.ffmpeg.org/msg10665

What's the correct way to close output file??! Every time there is something new added there are new leaks, couldn't there be some api that takes care of all that? At some point I had to add av_metadata_free and I have no idea what metadata and why I needed to free it. Allocated by internal functions and suddenly I need to free it myself. I never did that, but suddenly after some internal changes these structures became allocated internally and I needed to clean them up (Ffmpeg.c had that change made long time by the way : svn.16457 : free all allocated metadata structures).
To fix leaks caused by av_interleaved_write_frame (which dups packets internally)
I had to copy from avformat/utils.c this static function into my code:
/* XXX: suppress the packet queue */
static void flush_packet_queue(AVFormatContext *s)

I already have this to close output file, is there some extra 20-30 call that I'm missing? :) I use ffmpeg.c as a reference, do I also need to close all that chapters and all that stuff unrelated to what I do?



	if (video_ctx){
		avcodec_close(video_ctx);
		video_ctx = 0;
	}
	if (audio_ctx){
		avcodec_close(audio_ctx);
		audio_ctx = 0;
	}

	if(video_stream){
		av_metadata_free(&video_stream->metadata);
		av_free(video_stream->priv_data);
		av_free(video_stream->codec);
		av_free(video_stream);
		video_stream = 0;
	}
	if(audio_stream){
		av_metadata_free(&audio_stream->metadata);
		av_free(audio_stream->priv_data);
		av_free(audio_stream->codec);
		av_free(audio_stream);
		audio_stream = 0;
	}
	if(output_ctx){
		flush_packet_queue(output_ctx);              // <<< ------- copied from avformat/utils.c
		av_metadata_free(&output_ctx->metadata);
		av_free(output_ctx->priv_data);
		av_free(output_ctx);
		output_ctx = 0;
	}

...




More information about the ffmpeg-devel mailing list