[FFmpeg-devel] Small memleak when use AAC codec.

Igor Makarov igor
Sun Nov 29 14:43:21 CET 2009


I found small memory leak when using aac. This leak is 32 bytes per loop without
MEMALIGN_HACK, and 48 bytes with MEMALIGN.

Simple code:

#include <iostream>
extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}

AVOutputFormat *OutFormat;
AVFormatContext *FormatCtx;
AVCodecContext *VideoCodecCtx, *AudioCodecCtx;
AVCodec *VideoCodec, *AudioCodec;
AVStream *VideoStream, *AudioStream;

int main (int argc, char * const argv[]) {
	av_register_all();
	
	while(1) 
	{
		FormatCtx = avformat_alloc_context();
		OutFormat = guess_format(NULL, "test.3gp", NULL);
		FormatCtx->oformat = OutFormat;
		strncpy(FormatCtx->filename, "test.3gp", sizeof(FormatCtx->filename));
		
		VideoStream = av_new_stream(FormatCtx, 0);
		VideoCodecCtx = VideoStream->codec;
		VideoCodecCtx->codec_id     = CODEC_ID_H263;
		VideoCodecCtx->codec_type   = CODEC_TYPE_VIDEO;
		VideoCodecCtx->bit_rate     = 16000;
		VideoCodecCtx->width        = 176;
		VideoCodecCtx->height       = 144;
		VideoCodecCtx->time_base.den = 10;
		VideoCodecCtx->time_base.num = 1;	
		VideoCodecCtx->pix_fmt       = PIX_FMT_YUV420P;
		VideoCodecCtx->flags        |= CODEC_FLAG_QSCALE;
		VideoCodecCtx->gop_size      = 20;
		VideoCodecCtx->global_quality = (float)5 * (float)FF_QP2LAMBDA;
		VideoStream->quality          = (float)5;
		VideoStream->r_frame_rate.den = 10;
		VideoStream->r_frame_rate.num = 1;
		VideoCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
		
		VideoCodec = avcodec_find_encoder(VideoCodecCtx->codec_id);
		
		if(avcodec_open(VideoCodecCtx, VideoCodec) < 0) {
			printf("Fail open video\n");
			return 1;
		}
		
		AudioStream = av_new_stream(FormatCtx, 1);
		AudioCodecCtx = AudioStream->codec;
		AudioCodecCtx->codec_id		= CODEC_ID_AAC;
		AudioCodecCtx->codec_type	= CODEC_TYPE_AUDIO;
		AudioCodecCtx->bit_rate		= 32000;
		AudioCodecCtx->sample_rate	= 8000;
		AudioCodecCtx->channels		= 1;
		AudioCodecCtx->time_base= (AVRational){1, 8000};
		AudioCodecCtx->sample_fmt = SAMPLE_FMT_S16;
		
		AudioCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
		
		AudioCodec = avcodec_find_encoder(AudioCodecCtx->codec_id);
		
		if(avcodec_open(AudioCodecCtx, AudioCodec)) {
			printf("Fail open audio\n");
			return 2;
		}
		
		url_fopen(&FormatCtx->pb, "test.3gp",URL_WRONLY);
		av_write_header(FormatCtx);
				
		AVStream *st;
		
		av_write_trailer(FormatCtx);
		
		url_fclose(FormatCtx->pb);
		avcodec_close(VideoStream->codec);
		avcodec_close(AudioStream->codec);
		
		for(uint32_t i=0;i< FormatCtx->nb_streams;i++) {
			st = FormatCtx->streams[i];
			if (st->parser) {
				av_parser_close(st->parser);
				av_free_packet(&st->cur_pkt);
			}
			av_metadata_free(&st->metadata);
			av_free(st->index_entries);
			av_free(st->codec->extradata);
			av_free(st->codec);
			av_free(st->priv_data);
			av_free(st);
		}
		
		av_free(FormatCtx);		
		usleep(3000);
	}
	
    return 0;
}

I tried to understand exactly where the memory is lost, and Founded that the functions 
av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx) don't free context, so if add av_free(ctx); mem leak goes away.

Patch attached.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: psymodel.c.patch
Type: application/octet-stream
Size: 210 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20091129/7588d57f/attachment.obj>
-------------- next part --------------





More information about the ffmpeg-devel mailing list