ffmpeg: fix memory leak if writer is initialized multiple times.

This commit is contained in:
Adrien Destugues 2020-09-05 17:44:06 +02:00
parent 3cf5015980
commit 9a6a570ac9

View File

@ -93,6 +93,7 @@ AVFormatWriter::StreamCookie::StreamCookie(AVFormatContext* context,
AVFormatWriter::StreamCookie::~StreamCookie() AVFormatWriter::StreamCookie::~StreamCookie()
{ {
// fStream is freed automatically when the codec context is closed
} }
@ -392,22 +393,24 @@ AVFormatWriter::Init(const media_file_format* fileFormat)
{ {
TRACE("AVFormatWriter::Init()\n"); TRACE("AVFormatWriter::Init()\n");
uint8* buffer = static_cast<uint8*>(av_malloc(kIOBufferSize));
if (buffer == NULL)
return B_NO_MEMORY;
// Allocate I/O context and initialize it with buffer
// and hook functions, pass ourself as cookie.
fIOContext = avio_alloc_context(buffer, kIOBufferSize, 1, this,
0, _Write, _Seek);
if (fIOContext == NULL) { if (fIOContext == NULL) {
av_free(buffer); uint8* buffer = static_cast<uint8*>(av_malloc(kIOBufferSize));
TRACE("av_alloc_put_byte() failed!\n"); if (buffer == NULL)
return B_ERROR; return B_NO_MEMORY;
}
// Setup I/O hooks. This seems to be enough. // Allocate I/O context and initialize it with buffer
fFormatContext->pb = fIOContext; // and hook functions, pass ourself as cookie.
fIOContext = avio_alloc_context(buffer, kIOBufferSize, 1, this,
0, _Write, _Seek);
if (fIOContext == NULL) {
av_free(buffer);
TRACE("av_alloc_put_byte() failed!\n");
return B_ERROR;
}
// Setup I/O hooks. This seems to be enough.
fFormatContext->pb = fIOContext;
}
// Set the AVOutputFormat according to fileFormat... // Set the AVOutputFormat according to fileFormat...
fFormatContext->oformat = av_guess_format(fileFormat->short_name, fFormatContext->oformat = av_guess_format(fileFormat->short_name,