Memory leak while opening encoders in ffmpeg -
i'm getting memory leaks in avcodec_find_encoder. although i'm cleaning resources still i'm not able rid of leak. successive commenting code found memory leaks happen after call of avcodec_find_encoder(). i've tried code different video files , found memory leaks blocks same. if open audio or video 1 memory leaks block. below part of init , clean-up code application. note part of code contains initialization , resource release.
avformatcontext *m_informat; avformatcontext *m_outformat; avstream *m_in_vid_strm, *m_out_vid_strm; avstream *m_in_aud_strm, *m_out_aud_strm; int videoclipper::init(const wxstring& filename) { int ret = 0; char errbuf[64]; av_register_all(); if ((ret = avformat_open_input( &m_informat, filename.mb_str(), 0, 0)) != 0 ) { av_strerror(ret,errbuf,sizeof(errbuf)); print_val("not able open file;; ", errbuf) ret = -1; return ret; } else { print_msg("opened file ") } if ((ret = avformat_find_stream_info(m_informat, 0))< 0 ) { av_strerror(ret,errbuf,sizeof(errbuf)); print_val("not able find stream info:: ", errbuf) ret = -1; return ret; } else { print_msg("got stream info ") } for(unsigned int = 0; i<m_informat->nb_streams; i++) { if(m_informat->streams[i]->codec->codec_type == avmedia_type_video) { print_msg("found video stream ") m_in_vid_strm_idx = i; m_in_vid_strm = m_informat->streams[i]; } if(m_informat->streams[i]->codec->codec_type == avmedia_type_audio) { print_msg("found audio stream ") m_in_aud_strm_idx = i; m_in_aud_strm = m_informat->streams[i]; } } avoutputformat *outfmt = null; std::string outfile = std::string(filename) + "clip_out.avi"; outfmt = av_guess_format(null,outfile.c_str(),null); if(outfmt == null) { ret = -1; return ret; } else { m_outformat = avformat_alloc_context(); if(m_outformat) { m_outformat->oformat = outfmt; _snprintf(m_outformat->filename, sizeof(m_outformat->filename), "%s", outfile.c_str()); } else { ret = -1; return ret; } } avcodec *out_vid_codec,*out_aud_codec; out_vid_codec = out_aud_codec = null; if(outfmt->video_codec != av_codec_id_none && m_in_vid_strm != null) { out_vid_codec = avcodec_find_encoder(outfmt->video_codec); if(null == out_vid_codec) { print_msg("could not find vid encoder") ret = -1; return ret; } else { print_msg("found out vid encoder ") m_out_vid_strm = avformat_new_stream(m_outformat, out_vid_codec); if(null == m_out_vid_strm) { print_msg("failed allocate output vid strm ") ret = -1; return ret; } else { print_msg("allocated video stream ") if(avcodec_copy_context(m_out_vid_strm->codec, m_informat->streams[m_in_vid_strm_idx]->codec) != 0) { print_msg("failed copy context ") ret = -1; return ret; } } } } if(outfmt->audio_codec != av_codec_id_none && m_in_aud_strm != null) { out_aud_codec = avcodec_find_encoder(outfmt->audio_codec); if(null == out_aud_codec) { print_msg("could not find out aud encoder ") ret = -1; return ret; } else { print_msg("found out aud encoder ") m_out_aud_strm = avformat_new_stream(m_outformat, out_aud_codec); if(null == m_out_aud_strm) { print_msg("failed allocate out vid strm ") ret = -1; return ret; } else { if(avcodec_copy_context(m_out_aud_strm->codec, m_informat->streams[m_in_aud_strm_idx]->codec) != 0) { print_msg("failed copy context ") ret = -1; return ret; } } } } if (!(outfmt->flags & avfmt_nofile)) { if (avio_open2(&m_outformat->pb, outfile.c_str(), avio_flag_write,null, null) < 0) { print_val("could not open file ", outfile) ret = -1; return ret; } } /* write stream header, if any. */ if (avformat_write_header(m_outformat, null) < 0) { print_val("error occurred while writing header ", outfile) ret = -1; return ret; } else { print_msg("written output header ") m_init_done = true; } return ret; }
here clean-up part
void videoclipper::releaseresource(void) { if(m_in_aud_strm && m_in_aud_strm->codec) { avcodec_close(m_in_aud_strm->codec); print_msg("closed input audio codec ") } if(m_in_vid_strm && m_in_vid_strm->codec) { avcodec_close(m_in_vid_strm->codec); print_msg("closed input video codec ") } if(m_informat) { avformat_close_input(&m_informat); print_msg("freed input format contex ") } if(m_out_aud_strm && m_out_aud_strm->codec) { avcodec_close(m_out_aud_strm->codec); print_msg("closed output audio codec ") } if(m_out_vid_strm && m_out_vid_strm->codec) { avcodec_close(m_out_vid_strm->codec); print_msg("closed output audio codec ") } if(m_outformat) { avformat_close_input(&m_outformat); m_outformat = null; print_msg("closed output format ") } }
memory leaks message
detected memory leaks! dumping objects -> {13691} normal block @ 0x01046a60, 4479 bytes long. data: < > cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd {13685} normal block @ 0x01043fd0, 10831 bytes long. data: < ? > cd cd cd cd cd cd cd cd d0 3f 04 01 ed ed ed ed object dump complete.
i'm using latest version of ffmpeg on visual studio 2012. please suggest i'm missing.
thanks pradeep
there lots of thing matters here
first need close io
if (!(fmt->flags & avfmt_nofile)) { /* close output file. */ avio_close(ctx->oc->pb); }
you should call
avformat_free_context(ctx->oc);
there 24 bytes memory leakage @ system due allocation pthread_mutex in libavcodec/utils.c of ffmpeg, , there no way free memory.atleast till or fix code.
Comments
Post a Comment