Skip to content

Commit

Permalink
Fix few memory leaks
Browse files Browse the repository at this point in the history
Coverity #1046894
  • Loading branch information
jyavenard committed Jul 12, 2013
1 parent 37dde6f commit 3deabbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
30 changes: 24 additions & 6 deletions mythtv/libs/libmythtv/avformatwriter.cpp
Expand Up @@ -60,8 +60,8 @@ AVFormatWriter::~AVFormatWriter()

if (m_ctx)
{
av_write_trailer(m_ctx);
avio_close(m_ctx->pb);
(void)av_write_trailer(m_ctx);
avio_closep(&m_ctx->pb);
for(unsigned int i = 0; i < m_ctx->nb_streams; i++) {
av_freep(&m_ctx->streams[i]);
}
Expand All @@ -76,6 +76,8 @@ AVFormatWriter::~AVFormatWriter()

if (m_audPicture)
avcodec_free_frame(&m_audPicture);

Cleanup();
}

bool AVFormatWriter::Init(void)
Expand Down Expand Up @@ -171,6 +173,7 @@ bool AVFormatWriter::OpenFile(void)

if (!m_ringBuffer)
{
Cleanup();
LOG(VB_RECORD, LOG_ERR, LOC +
"OpenFile(): RingBuffer::Create() failed");
return false;
Expand All @@ -181,23 +184,38 @@ bool AVFormatWriter::OpenFile(void)
uc->prot = AVFRingBuffer::GetRingBufferURLProtocol();
uc->priv_data = (void *)m_avfRingBuffer;

avformat_write_header(m_ctx, NULL);
if (avformat_write_header(m_ctx, NULL) < 0)
{
Cleanup();
return false;
}

return true;
}

void AVFormatWriter::Cleanup(void)
{
if (m_ctx && m_ctx->pb)
{
avio_closep(&m_ctx->pb);
}
delete m_avfRingBuffer;
m_avfRingBuffer = NULL;
delete m_ringBuffer;
m_ringBuffer = NULL;
}

bool AVFormatWriter::CloseFile(void)
{
if (m_ctx)
{
av_write_trailer(m_ctx);
(void)av_write_trailer(m_ctx);
avio_close(m_ctx->pb);
for(unsigned int i = 0; i < m_ctx->nb_streams; i++) {
av_freep(&m_ctx->streams[i]);
}

av_free(m_ctx);
m_ctx = NULL;
av_freep(&m_ctx);
}

return true;
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythtv/avformatwriter.h
Expand Up @@ -37,6 +37,7 @@ class MTV_PUBLIC AVFormatWriter : public FileWriterBase
AVStream *AddAudioStream(void);
bool OpenAudio(void);
AVFrame *AllocPicture(enum PixelFormat pix_fmt);
void Cleanup(void);

AVRational GetCodecTimeBase(void);
bool FindAudioFormat(AVCodecContext *ctx, AVCodec *c, AVSampleFormat format);
Expand Down

0 comments on commit 3deabbd

Please sign in to comment.