Small memleak when use AAC codec.
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.
On Sun, Nov 29, 2009 at 04:43:21PM +0300, Igor Makarov wrote:
I found small memory leak when using aac. This leak is 32 bytes per loop without MEMALIGN_HACK, and 48 bytes with MEMALIGN. [...] 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. psymodel.c | 1 + 1 file changed, 1 insertion(+) 642a68f3c0331e3156650fdc02421f0e0b5a78d7 psymodel.c.patch +++ ./libavcodec/psymodel.c @@ -126,5 +126,6 @@ for (i = 0; i < ctx->avctx->channels; i++) ff_iir_filter_free_state(ctx->fstate[i]); av_freep(&ctx->fstate); + av_free(ctx); }
it appears this has been fixed [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Many that live deserve death. And some that die deserve life. Can you give it to them? Then do not be too eager to deal out death in judgement. For even the very wise cannot see all ends. -- Gandalf
participants (2)
-
igor@plusmobile.ru -
michaelni@gmx.at