From: Raja Rathour via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Raja Rathour <imraja729@gmail.com>
Subject: [FFmpeg-devel] [PATCH 2/4] avfilter/dnn_backend_torch: fix memory leak with persistent input buffer
Date: Tue, 20 Jan 2026 19:39:37 +0530
Message-ID: <20260120140939.32403-2-imraja729@gmail.com> (raw)
In-Reply-To: <20260120140939.32403-1-imraja729@gmail.com>
---
libavfilter/dnn/dnn_backend_torch.cpp | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp
index 4c781cc0b6..fee1cfb3a7 100644
--- a/libavfilter/dnn/dnn_backend_torch.cpp
+++ b/libavfilter/dnn/dnn_backend_torch.cpp
@@ -47,6 +47,8 @@ typedef struct THModel {
typedef struct THInferRequest {
torch::Tensor *output;
torch::Tensor *input_tensor;
+ float *input_data; ///< persistent buffer for input data
+ size_t input_data_size; ///< size of the persistent buffer
} THInferRequest;
typedef struct THRequestItem {
@@ -94,6 +96,7 @@ static void th_free_request(THInferRequest *request)
delete(request->input_tensor);
request->input_tensor = NULL;
}
+ // Add these lines to free the persistent buffer
if (request->input_data) {
av_freep(&request->input_data);
request->input_data_size = 0;
@@ -199,17 +202,12 @@ static int fill_model_input_th(THModel *th_model, THRequestItem *request)
input.dims[height_idx] = task->in_frame->height;
input.dims[width_idx] = task->in_frame->width;
- // Calculate required size for the current frame
- cur_size = input.dims[height_idx] * input.dims[width_idx] *
- input.dims[channel_idx] * sizeof(float);
-
- /**
- * Dynamic Resizing Logic:
- * Only reallocate if the existing buffer is too small or doesn't exist.
- * Removed the (float *) cast to comply with FFmpeg style guidelines.
- */
- if (!infer_request->input_data || infer_request->input_data_size < cur_size) {
- av_freep(&infer_request->input_data);
+ // Calculate required size for current frame
+ size_t cur_size = input.dims[height_idx] * input.dims[width_idx] *
+ input.dims[channel_idx] * sizeof(float);
+
+ // Initial allocation
+ if (!infer_request->input_data) {
infer_request->input_data = av_malloc(cur_size);
if (!infer_request->input_data)
return AVERROR(ENOMEM);
--
2.51.0
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
next prev parent reply other threads:[~2026-01-20 14:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 14:09 [FFmpeg-devel] [PATCH 1/4] avfilter/dnn_backend_torch: implement common async infrastructure Raja Rathour via ffmpeg-devel
2026-01-20 14:09 ` Raja Rathour via ffmpeg-devel [this message]
2026-01-20 14:09 ` [FFmpeg-devel] [PATCH 3/4] avfilter/dnn_backend_torch: add support for dynamic input shapes Raja Rathour via ffmpeg-devel
2026-01-20 14:09 ` [FFmpeg-devel] [PATCH 4/4] avfilter/dnn_backend_torch: improve device selection and error handling Raja Rathour via ffmpeg-devel
2026-01-21 2:08 ` [FFmpeg-devel] Re: [PATCH 1/4] avfilter/dnn_backend_torch: implement common async infrastructure Guo, Yejun via ffmpeg-devel
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=20260120140939.32403-2-imraja729@gmail.com \
--to=ffmpeg-devel@ffmpeg.org \
--cc=imraja729@gmail.com \
/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