[Libav-user] Memory leak while adding new streams

Pradeep Karosiya praks411 at gmail.com
Tue May 21 16:14:33 CEST 2013


Hi,

I have been struggling for quite some time because of memory leaks which I'm
getting while encoding video and audio frames. I have posted earlier also
regarding the same issue but couldn't get much clue. 
So I've broken down my problem into smaller parts. 
Till now I've found that as soon as I added new stream to output file. I get
a memory leak.
I've tried all the possible way of freeing streams, closing codec and
closing AVFormatContext(listed in examples/suggested in forum) but still the
leak persist. 
Below is my small test program. I'm opening input file, probing the stream ,
allocating avformat_alloc_context() and adding only one stream. Finally
closing everything. Still I'm getting memory leaks of 10831 bytes.
Please suggest.

int main(int argc, char *argv[])
{
     AVFormatContext *informat, *outformat;
     informat  = outformat = NULL;
      AVOutputFormat *outfmt = NULL;

    int in_aud_strm_idx, in_vid_strm_idx, out_aud_strm_idx,
out_vid_strm_idx;
    out_aud_strm_idx = out_vid_strm_idx = in_aud_strm_idx = in_vid_strm_idx
= -1;

    AVStream *in_aud_strm, *in_vid_strm, *out_aud_strm, *out_vid_strm;
    out_aud_strm = out_vid_strm = in_aud_strm = in_vid_strm = NULL;

    AVCodecContext *in_aud_codec_ctx, *in_vid_codec_ctx, *out_aud_codec_ctx,
*out_vid_codec_ctx;
    out_aud_codec_ctx = out_vid_codec_ctx = in_aud_codec_ctx =
in_vid_codec_ctx = NULL;

    AVCodec *in_aud_codec, *in_vid_codec, *out_aud_codec, *out_vid_codec;
    out_aud_codec = out_vid_codec = in_aud_codec = in_vid_codec = NULL;
    av_register_all();
     const char *sourcefile = "infile1.flv";
    const char *outfile = "outfile1.avi";
      if ( (ret = avformat_open_input( &informat, sourcefile, 0, 0)) != 0 )
    {
        PRINT_VAL("Not able to Open file;; ", errbuf)
        std::getchar();
        return -1;
    }
 if ((ret = avformat_find_stream_info(informat, 0))< 0 )
    {
        PRINT_VAL("Not Able to find stream info:: ", errbuf)
        std::getchar();
        return -1;
    }
    else
    {
        PRINT_MSG("Got stream Info ")
    }
    else
    {
        PRINT_MSG("Opened File ")
    }
 for(unsigned int i = 0; i<informat->nb_streams; i++)
    {
        if(informat->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
   
            PRINT_MSG("Found Video Stream ")
            in_vid_strm_idx = i;
            in_vid_strm = informat->streams[i];
        }

        if(informat->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            PRINT_MSG("Found Audio Stream ")
            in_aud_strm_idx = i;
            in_aud_strm = informat->streams[i];

        }
    }

        outfmt = av_guess_format(NULL, (const char*)outfile, NULL);

        if(NULL == outfmt)
        {
            PRINT_MSG("Not able to Guess Output format ")
            std::getchar();
        }
        else
        {
            outformat = avformat_alloc_context();
            if(outformat)
            {
                PRINT_MSG("Got Output context ")
                outformat->oformat = outfmt;
                _snprintf(outformat->filename, sizeof(outformat->filename),
"%s", (const char*)outfile);
                if(outfmt->video_codec != AV_CODEC_ID_NONE)
                {
                    out_vid_codec =
avcodec_find_encoder(outfmt->video_codec);
                    if(NULL == out_vid_codec)
                    {
                        PRINT_MSG("Could Not Find Vid Encoder")
                    }
                    else
                    {

                        PRINT_MSG("Found Out Vid Encoder ")
                        out_vid_strm = avformat_new_stream(outformat,
out_vid_codec);
                        if(NULL == out_vid_strm)
                        {
                            PRINT_MSG("Failed to Allocate Output Vid Strm ")
                        }
                        else
                        {

                            PRINT_MSG("Allocated Video Stream ")
                            if(avcodec_copy_context(out_vid_strm->codec,
in_vid_codec_ctx) != 0)
                            {
                                PRINT_MSG("Failed to Copy Context ")
                            }
                            else
                            {
                                PRINT_MSG("Copied Context ")
                            }
                          }
                        }
         
                    }

     if(informat)
    {
        if(in_aud_codec_ctx)
       {
           avcodec_close(in_aud_codec_ctx);    
       }

    if(in_vid_codec_ctx)
    {
        avcodec_close(in_vid_codec_ctx);    
    }
     avformat_close_input(&informat);
     av_free(informat);
    }
  
  if(outformat)
      {
        if(out_aud_codec_ctx)
    {
        avcodec_close(out_aud_codec_ctx);    
     
    }
    if(out_vid_codec_ctx)
    {
        avcodec_close(out_vid_codec_ctx);
    }
     if(outformat->pb)
        {
            avio_close(outformat->pb);
        }
    avformat_free_context(outformat);
    
    //av_free(&outformat);
    std::cout<<"Freeing "<<std::endl;
      }

}
Thanks 
Pradeep



--
View this message in context: http://libav-users.943685.n4.nabble.com/Memory-leak-while-adding-new-streams-tp4657663.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list