From 72c6787b503eef8769ce38e22eaf02dfe830ce21 Mon Sep 17 00:00:00 2001 From: Fredrik Mellbin Date: Sat, 21 Sep 2024 12:41:16 +0200 Subject: [PATCH] Fix tiny memory leak if any of the decoder or demuxer options are set and opening a file fails --- src/audiosource.cpp | 4 +++- src/tracklist.cpp | 5 ++++- src/videosource.cpp | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/audiosource.cpp b/src/audiosource.cpp index 19eaccd..4ac003f 100644 --- a/src/audiosource.cpp +++ b/src/audiosource.cpp @@ -77,8 +77,10 @@ void LWAudioDecoder::OpenFile(const std::filesystem::path &SourceFile, int Track for (const auto &Iter : LAVFOpts) av_dict_set(&Dict, Iter.first.c_str(), Iter.second.c_str(), 0); - if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) + if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) { + av_dict_free(&Dict); throw BestSourceException("Couldn't open '" + SourceFile.u8string() + "'"); + } av_dict_free(&Dict); diff --git a/src/tracklist.cpp b/src/tracklist.cpp index 6bc47a0..5c52a53 100644 --- a/src/tracklist.cpp +++ b/src/tracklist.cpp @@ -20,6 +20,7 @@ #include "tracklist.h" #include +#include extern "C" { #include @@ -31,8 +32,10 @@ void BestTrackList::OpenFile(const std::filesystem::path &SourceFile, const std: for (const auto &Iter : LAVFOpts) av_dict_set(&Dict, Iter.first.c_str(), Iter.second.c_str(), 0); - if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) + if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) { + av_dict_free(&Dict); throw BestSourceException("Couldn't open '" + SourceFile.u8string() + "'"); + } av_dict_free(&Dict); diff --git a/src/videosource.cpp b/src/videosource.cpp index b1b2d21..adea3f9 100644 --- a/src/videosource.cpp +++ b/src/videosource.cpp @@ -131,8 +131,10 @@ void LWVideoDecoder::OpenFile(const std::filesystem::path &SourceFile, const std for (const auto &Iter : LAVFOpts) av_dict_set(&Dict, Iter.first.c_str(), Iter.second.c_str(), 0); - if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) + if (avformat_open_input(&FormatContext, SourceFile.u8string().c_str(), nullptr, &Dict) != 0) { + av_dict_free(&Dict); throw BestSourceException("Couldn't open '" + SourceFile.u8string() + "'"); + } av_dict_free(&Dict);