From 53c84054f14dd98aecea44c2d2b5c76ea95347a9 Mon Sep 17 00:00:00 2001 From: Alexander Kraynikov Date: Wed, 10 Sep 2025 17:46:15 +0300 Subject: [PATCH 1/2] add cudaFree d_counter to avoid memory leak --- modules/cudafeatures2d/src/fast.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/cudafeatures2d/src/fast.cpp b/modules/cudafeatures2d/src/fast.cpp index e2c13b06b2..d709a77911 100644 --- a/modules/cudafeatures2d/src/fast.cpp +++ b/modules/cudafeatures2d/src/fast.cpp @@ -141,6 +141,7 @@ namespace if (count == 0) { _keypoints.release(); + cudaSafeCall( cudaFree(d_counter) ); return; } From 3f97acf75a6993150df847799c99307f96d547ea Mon Sep 17 00:00:00 2001 From: Alexander Kraynikov <65544047+incubus-ank@users.noreply.github.com> Date: Fri, 12 Sep 2025 17:07:52 +0300 Subject: [PATCH 2/2] Moved cudaMalloc() and cudaFree() to the constructor and destructor to avoid memory reallocation --- modules/cudafeatures2d/src/fast.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/cudafeatures2d/src/fast.cpp b/modules/cudafeatures2d/src/fast.cpp index d709a77911..5138ec0b89 100644 --- a/modules/cudafeatures2d/src/fast.cpp +++ b/modules/cudafeatures2d/src/fast.cpp @@ -66,6 +66,7 @@ namespace { public: FAST_Impl(int threshold, bool nonmaxSuppression, int max_npoints); + ~FAST_Impl(); virtual void detect(InputArray _image, std::vector& keypoints, InputArray _mask); virtual void detectAsync(InputArray _image, OutputArray _keypoints, InputArray _mask, Stream& stream); @@ -95,6 +96,12 @@ namespace FAST_Impl::FAST_Impl(int threshold, bool nonmaxSuppression, int max_npoints) : threshold_(threshold), nonmaxSuppression_(nonmaxSuppression), max_npoints_(max_npoints) { + cudaSafeCall( cudaMalloc(&d_counter, sizeof(unsigned int)) ); + } + + FAST_Impl::~FAST_Impl() + { + cudaSafeCall( cudaFree(d_counter) ); } void FAST_Impl::detect(InputArray _image, std::vector& keypoints, InputArray _mask) @@ -116,8 +123,6 @@ namespace { using namespace cv::cuda::device::fast; - cudaSafeCall( cudaMalloc(&d_counter, sizeof(unsigned int)) ); - const GpuMat img = _image.getGpuMat(); const GpuMat mask = _mask.getGpuMat(); @@ -141,7 +146,6 @@ namespace if (count == 0) { _keypoints.release(); - cudaSafeCall( cudaFree(d_counter) ); return; } @@ -166,8 +170,6 @@ namespace kpLoc.colRange(0, count).copyTo(locRow, stream); keypoints.row(1).setTo(Scalar::all(0), stream); } - - cudaSafeCall( cudaFree(d_counter) ); } void FAST_Impl::convert(InputArray _gpu_keypoints, std::vector& keypoints)