From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 7E8794C921 for ; Wed, 6 Aug 2025 16:21:36 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id E8CE468CB11; Wed, 6 Aug 2025 19:21:31 +0300 (EEST) Received: from mail-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 0C6A5687C3F for ; Wed, 6 Aug 2025 19:21:26 +0300 (EEST) Received: by mail-il1-f176.google.com with SMTP id e9e14a558f8ab-3e51e6fcf65so479235ab.3 for ; Wed, 06 Aug 2025 09:21:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754497284; x=1755102084; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qAv8Q9fbyE0RNsD5UtfFRkOQx2wbWw/OTOuyCwvYwVI=; b=BSs+pX5Ul/Kjpf2/6oeUyu71yJEAfGazbVTqYK1BbNBF1YBgGicz8e+qf48gP/n6AQ g8WYwD049CoipqV7j0Sg/wJn+x2iDxpOTr1xpeLegJNCSDPkoaeK2LJGYq+dyj1RQKPn fhIpMM3OI4wtO0vNzHfujYT5ju12at1zlChFAffHNugg8qFBnkZ7OFZKIjvh9ujGV5G9 9oc7vsU7XfqQcjAOPjpRQLzep2MZPeVCfjYh+fMMtS6TSeSnGPZSiNqd0Px1CLhxCifm GraeYGY2MEcZdqqmSB5fFMNtZw99AZt37Cu6VdVAIKpJvhvBA6XgIOcf3HzkHLtyVUOc ER/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754497284; x=1755102084; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qAv8Q9fbyE0RNsD5UtfFRkOQx2wbWw/OTOuyCwvYwVI=; b=uGf3u+mzJwxGOfWNsK17rf23JdNEEXLmaqRHQmKFZHjSja6z/chiJ63osTmCsYK3sO Jr5liOLfWm0tDTXVC8ONothwlbmHR8oDei1onHL/Jyq2LmaM00ajvLnxtVgfE3Rhj/1D QSmFMh1rHpygsmt1UzFmwr/8NNk1vyNsDwddFXtAeE5Q4WaInmqCg1lT1+AMjXDXgwnR dJuTchRH1h60DNdymWgZ4DFedbPxNYd+VbaeDDoNZTTB8kdB9uzIqriV7R40oVlP+PJe E+KC4XJ1+SGkERmfNbG65jUG1/vX/jupBYxSlRvOqvZluzQ95HpYiG9oR0dyG9LQIVHf AOaA== X-Gm-Message-State: AOJu0YxMn6sl+I6L04Fm1sVeQ6Qi9Sln2y9blXyESUGGX24Dm1vsBEa3 PZL++SjdsnLM0NQYcrBpT1yymTaMtzgjTp05ozPNsag29OpCcmTCQEBVhQ5Xog== X-Gm-Gg: ASbGncvnyITgTspPiL6R+cAFeps4m7z1Q25BFsefBYDYH+xySevy92UOj3BmLiDFoi8 OQu+0h5siVSHud6MATN3EpQ04WjF7T0P999fxBQ3i3Zbev8/sLQ+tW+1jeHAfrSdi7wxj6YT0FX jNPywaBXxqOdRPID1B/uuiPppajMMfcPZZF9wantt/Mfk2grWf1e/CY7kARMzIBFAHng7oDtjbK V5QQRFHw61fchzuHS5cKqiEPmx69uz2YDci6Bk77Db+VpNghKS/QZMxIaXWCiQKSekDDf4XpdUj piGI8Fhq45ImK/4x8x1lM1By60RmgPvL1y+z/2IVXv4agV4I0Bff56JKvoigpP+GgtpmspvpLyA 9omFbZg/k6U5uhOo/XKZeEg/LL3Yf3Aqy4gB7FNbj86M/ X-Google-Smtp-Source: AGHT+IGuibjSvYpYOr65zUg7x87Rs5LjB+rEVLMTA4dwpqWdSzUAWEtrMQ1a6NMmKHF7t9NvIiifrQ== X-Received: by 2002:a05:6e02:3f0b:b0:3e2:c350:2e61 with SMTP id e9e14a558f8ab-3e51b9318ddmr58202315ab.20.1754497283856; Wed, 06 Aug 2025 09:21:23 -0700 (PDT) Received: from newman.cs.purdue.edu ([128.10.127.250]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-50a5db1e956sm4733862173.34.2025.08.06.09.21.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Aug 2025 09:21:23 -0700 (PDT) From: Jiasheng Jiang To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Aug 2025 16:21:20 +0000 Message-Id: <20250806162120.33857-1-jiashengjiangcool@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] doc/examples/hw_decode: Add proper deallocations to avoid potential memory leaks X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Jiasheng Jiang Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Add proper deallocations in the error paths to avoid potential memory leaks. Fixes: 1e0c75ea16 ("examples/hw_decode: Add a HWAccel decoding example.") Signed-off-by: Jiasheng Jiang --- doc/examples/hw_decode.c | 48 +++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/doc/examples/hw_decode.c b/doc/examples/hw_decode.c index ac4e445505..4324b1f18d 100644 --- a/doc/examples/hw_decode.c +++ b/doc/examples/hw_decode.c @@ -181,19 +181,22 @@ int main(int argc, char *argv[]) /* open the input file */ if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) { fprintf(stderr, "Cannot open input file '%s'\n", argv[2]); - return -1; + ret = -1; + goto free_packet; } if (avformat_find_stream_info(input_ctx, NULL) < 0) { fprintf(stderr, "Cannot find input stream information.\n"); - return -1; + ret = -1; + goto close_input; } /* find the video stream information */ ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0); if (ret < 0) { fprintf(stderr, "Cannot find a video stream in the input file\n"); - return -1; + ret = -1; + goto close_input; } video_stream = ret; @@ -202,7 +205,8 @@ int main(int argc, char *argv[]) if (!config) { fprintf(stderr, "Decoder %s does not support device type %s.\n", decoder->name, av_hwdevice_get_type_name(type)); - return -1; + ret = -1; + goto close_input; } if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && config->device_type == type) { @@ -211,21 +215,28 @@ int main(int argc, char *argv[]) } } - if (!(decoder_ctx = avcodec_alloc_context3(decoder))) - return AVERROR(ENOMEM); + if (!(decoder_ctx = avcodec_alloc_context3(decoder))) { + ret = AVERROR(ENOMEM); + goto close_input; + } video = input_ctx->streams[video_stream]; - if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) - return -1; + if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) { + ret = -1; + goto free_decoder_ctx; + } decoder_ctx->get_format = get_hw_format; - if (hw_decoder_init(decoder_ctx, type) < 0) - return -1; + if (hw_decoder_init(decoder_ctx, type) < 0) { + ret -1; + goto free_decoder_ctx; + } if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream); - return -1; + ret = -1; + goto av_frame_unref; } /* open the file to dump raw data */ @@ -243,14 +254,21 @@ int main(int argc, char *argv[]) } /* flush the decoder */ - ret = decode_write(decoder_ctx, NULL); + decode_write(decoder_ctx, NULL); if (output_file) fclose(output_file); - av_packet_free(&packet); + + ret = 0; + +av_frame_unref: + av_buffer_unref(&hw_device_ctx); +free_decoder_ctx: avcodec_free_context(&decoder_ctx); +close_input: avformat_close_input(&input_ctx); - av_buffer_unref(&hw_device_ctx); +free_packet: + av_packet_free(&packet); - return 0; + return ret; } -- 2.25.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".