Skip to content

Commit

Permalink
Fix file leak in MovieTexture_FFMpeg.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed May 26, 2022
1 parent 0ed44ae commit 7dcf142
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/arch/MovieTexture/MovieTexture_FFMpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ MovieDecoder_FFMpeg::MovieDecoder_FFMpeg()
{
FixLilEndian();

m_swsctx = NULL;
m_avioContext = NULL;
m_buffer = NULL;
m_fctx = NULL;
m_pStream = NULL;
m_iCurrentPacketOffset = -1;
Expand All @@ -130,43 +133,44 @@ MovieDecoder_FFMpeg::MovieDecoder_FFMpeg()
}

MovieDecoder_FFMpeg::~MovieDecoder_FFMpeg()
{
Init();
}

void
MovieDecoder_FFMpeg::Init()
{
if (m_iCurrentPacketOffset != -1) {
avcodec::av_free_packet(&m_Packet);
m_iCurrentPacketOffset = -1;
}

m_iEOF = 0;
m_fTimestamp = 0;
m_fLastFrameDelay = 0;
m_iFrameNumber = -1; /* decode one frame and you're on the 0th */
m_fTimestampOffset = 0;
m_fLastFrame = 0;
if (m_swsctx) {
avcodec::sws_freeContext(m_swsctx);
m_swsctx = NULL;
}
m_swsctx = NULL;
// Note: m_avioContext->buffer refers to m_buffer, but ffmpeg sometimes
// reallocates the buffer to change the size, and in that case our m_buffer
// pointer is freed already, so we instead check m_avioContext->buffer
// to free m_buffer
if (m_avioContext && m_avioContext->buffer != NULL) {
avcodec::av_free(m_avioContext->buffer);
}
m_buffer = NULL;
if (m_avioContext != NULL) {
RageFile* file = (RageFile*)m_avioContext->opaque;
file->Close();
delete file;
avcodec::av_free(m_avioContext);
}
if (m_buffer != NULL) {
avcodec::av_free(m_buffer);
}
}

void
MovieDecoder_FFMpeg::Init()
{
m_iEOF = 0;
m_fTimestamp = 0;
m_fLastFrameDelay = 0;
m_iFrameNumber = -1; /* decode one frame and you're on the 0th */
m_fTimestampOffset = 0;
m_fLastFrame = 0;
m_swsctx = NULL;
m_avioContext = NULL;
m_buffer = NULL;

if (m_iCurrentPacketOffset != -1) {
avcodec::av_free_packet(&m_Packet);
m_iCurrentPacketOffset = -1;
}
}

/* Read until we get a frame, EOF or error. Return -1 on error, 0 on EOF, 1 if
Expand Down Expand Up @@ -427,6 +431,8 @@ MovieDecoder_FFMpeg::Open(const std::string& sFile)
{
MovieTexture_FFMpeg::RegisterProtocols();

Close();

m_fctx = avcodec::avformat_alloc_context();
if (!m_fctx)
return "AVCodec: Couldn't allocate context";
Expand Down Expand Up @@ -487,7 +493,17 @@ MovieDecoder_FFMpeg::Open(const std::string& sFile)
std::string
MovieDecoder_FFMpeg::OpenCodec()
{
Init();
if (m_iCurrentPacketOffset != -1) {
avcodec::av_free_packet(&m_Packet);
m_iCurrentPacketOffset = -1;
}

m_iEOF = 0;
m_fTimestamp = 0;
m_fLastFrameDelay = 0;
m_iFrameNumber = -1; /* decode one frame and you're on the 0th */
m_fTimestampOffset = 0;
m_fLastFrame = 0;

ASSERT(m_pStream != NULL);
if (m_pStream->codec->codec)
Expand Down

0 comments on commit 7dcf142

Please sign in to comment.