[Libav-user] Memory leaks when opening RTMP stream with wrong url

Антон Евмененко anton.evmenenko.93 at gmail.com
Tue Nov 3 18:18:46 CET 2015


Hi everyone.

I'm using libav for streaming to YouTube through RTMP. I have function
openRtmpStream() for opening RTMP stream. It works well. But recently i
noticed the situation, when RTMP url is wrong (or server is down). In this
situation my code is trying to reconnect, and calls this function about 15
times per second. With htop i can see about 20 MB per second memory leak in
this situation.

Maybe my code for closing context and streams is wrong? Or any another idea
what I'm doing wrong? Thanks in advance!

Here's my code:

bool openRtmpStream( const std::string& address )
{
    if( ! m_httpContext ) {
        m_httpContext = avformat_alloc_context( );

        m_httpContext->oformat = av_guess_format( "flv", address.c_str( ),
nullptr );
        if( m_httpContext->oformat ) {
            strcpy( m_httpContext->filename, address.c_str( ) );

            auto codecID = AV_CODEC_ID_H264;

            auto codec = avcodec_find_encoder( codecID );

            if( codec ) {
                m_httpVideoStream = avformat_new_stream( m_httpContext,
codec );

                // ... here's initalization of m_httpVideoStream->codec ...

                int res = avcodec_open2( codecContext, codec, nullptr );
                if( res >= 0 ) {
                    auto codecID = AV_CODEC_ID_MP3;
                    auto codec = avcodec_find_encoder( codecID );
                    if( codec ) {
                        m_httpAudioStream = avformat_new_stream(
m_httpContext, codec );

                        // ... here's initalization of
m_httpAudioStream->codec ...

                        res = avcodec_open2( codecContext, codec, nullptr );
                        if( res >= 0 ) {
                            m_httpStreamWriteStartTime =
boost::chrono::high_resolution_clock::now( );
                            if( avio_open2( &m_httpContext->pb,
m_httpContext->filename, AVIO_FLAG_WRITE, m_AVIOInterruptCB.get( ), nullptr
) >= 0 ) {
                                if( avformat_write_header( m_httpContext,
nullptr ) >= 0 ) {
                                    return true; // success
                                }
                            }
                            avcodec_close( m_httpAudioStream->codec );
                        }
                    }
                    avcodec_close( m_httpVideoStream->codec );
                }
            }
        }

        // failed to open stream, close context and streams

        avio_close( m_httpContext->pb );
        avformat_free_context( m_httpContext );
        m_httpContext = nullptr;
        m_httpVideoStream = nullptr;
        m_httpAudioStream = nullptr;
    }
    return false;
}

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20151103/23a66289/attachment.html>


More information about the Libav-user mailing list