From 999887687c5e406fb8cd8597d2be9fa09a862002 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Sun, 22 Mar 2020 12:19:11 -0400 Subject: [PATCH] FFmpegWriter: Free any old context before clobbering valgrind caught that AVFORMAT_NEW_STREAM() could be leaking pointers when calling avcodec_alloc_context3(), if there's an existing context already assigned. So we check and free it first, if necessary. --- src/FFmpegWriter.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index 54b5bc850..eb100095c 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -1067,6 +1067,11 @@ AVStream *FFmpegWriter::add_audio_stream() { if (codec == NULL) throw InvalidCodec("A valid audio codec could not be found for this file.", path); + // Free any previous memory allocations + if (audio_codec_ctx != NULL) { + AV_FREE_CONTEXT(audio_codec_ctx); + } + // Create a new audio stream AV_FORMAT_NEW_STREAM(oc, audio_codec_ctx, codec, st)