From a9edb4de28c2d34b051e84558e09fe3d497900e7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 29 Jul 2016 09:08:14 +0200 Subject: [PATCH] decoder/ffmpeg: use AtScopeExit() for safe cleanup --- src/decoder/plugins/FfmpegDecoderPlugin.cxx | 39 +++++++++++++-------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx index b1cb32bed..9300d44f8 100644 --- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx +++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx @@ -35,6 +35,7 @@ #include "tag/TagHandler.hxx" #include "input/InputStream.hxx" #include "CheckAudioFormat.hxx" +#include "util/ScopeExit.hxx" #include "util/ConstBuffer.hxx" #include "util/Error.hxx" #include "LogV.hxx" @@ -473,6 +474,10 @@ FfmpegDecode(Decoder &decoder, InputStream &input, return; } + AtScopeExit(codec_context) { + avcodec_close(codec_context); + }; + const SignedSongTime total_time = FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base); @@ -489,6 +494,16 @@ FfmpegDecode(Decoder &decoder, InputStream &input, return; } + AtScopeExit(&frame) { +#if LIBAVUTIL_VERSION_MAJOR >= 53 + av_frame_free(&frame); +#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0) + avcodec_free_frame(&frame); +#else + av_free(frame); +#endif + }; + FfmpegBuffer interleaved_buffer; uint64_t min_frame = 0; @@ -537,16 +552,6 @@ FfmpegDecode(Decoder &decoder, InputStream &input, av_free_packet(&packet); #endif } - -#if LIBAVUTIL_VERSION_MAJOR >= 53 - av_frame_free(&frame); -#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0) - avcodec_free_frame(&frame); -#else - av_free(frame); -#endif - - avcodec_close(codec_context); } static void @@ -572,9 +577,11 @@ ffmpeg_decode(Decoder &decoder, InputStream &input) return; } - FfmpegDecode(decoder, input, *format_context); + AtScopeExit(&format_context) { + avformat_close_input(&format_context); + }; - avformat_close_input(&format_context); + FfmpegDecode(decoder, input, *format_context); } static bool @@ -619,9 +626,11 @@ ffmpeg_scan_stream(InputStream &is, if (f == nullptr) return false; - bool result = FfmpegScanStream(*f, *handler, handler_ctx); - avformat_close_input(&f); - return result; + AtScopeExit(&f) { + avformat_close_input(&f); + }; + + return FfmpegScanStream(*f, *handler, handler_ctx); } /**