From: Jiasheng Jiang <jiashengjiangcool@gmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Jiasheng Jiang <jiashengjiangcool@gmail.com> Subject: [FFmpeg-devel] [PATCH] doc/examples/hw_decode: Add proper deallocations to avoid potential memory leaks Date: Wed, 6 Aug 2025 16:21:20 +0000 Message-ID: <20250806162120.33857-1-jiashengjiangcool@gmail.com> (raw) 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 <jiashengjiangcool@gmail.com> --- 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".
reply other threads:[~2025-08-06 16:21 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250806162120.33857-1-jiashengjiangcool@gmail.com \ --to=jiashengjiangcool@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git