User error of cuda::TemplateMatching? or actual Memory leak?

I have been building an application that uses cuda::TemplateMatching, after some testing found the application crashes due to a memory leak. I created a sample test to verify it was nothing caused by code from my application and sure enough, it still has the same issue. Could anyone verify that I’m correctly using cuda::TemplateMatching? I feel like I’m missing something but I have gone through the documentation, examples, and even the source code and can not figure out where is my error. In advance thank you!

#include "opencv2/cudaimgproc.hpp"
#include "opencv2/opencv.hpp"

using namespace cv;

int main()
{
    Mat image, imageTemplate;
    cuda::GpuMat gpuResult;

    //Load images from file
    image = imread("Sunflower.jpg",IMREAD_GRAYSCALE);
    if (image.empty())
    {
        return 1;
    }
    imageTemplate = imread("SunflowerTemplate.jpg", IMREAD_GRAYSCALE);
    if (imageTemplate.empty())
    {
        return 2;
    }
    
    //Load images to gpuMat
    cuda::GpuMat gpuImage(image);
    cuda::GpuMat gpuTemplate(imageTemplate);

    cv::Ptr<cuda::TemplateMatching> mapMatching = cv::cuda::createTemplateMatching(CV_8U, TM_SQDIFF, Size(0, 0));

    for (int a = 0; a <= 9000; a++)
    {
        mapMatching->match(gpuImage, gpuTemplate, gpuResult);
    }
    return 0;
}
1 Like

Which version of OpenCV are CUDA are you using and what is the error you are recieving?

1 Like

The issue was fixed after building OpenCV 4.8 with Cuda. I was using OpenCV4.6, and my Cuda version is 12.2. The program worked correctly but after running for 1-2 hrs my process memory would continue increasing until eventually I would run out of memory and the program would just shut down. Now I run the same test sample and my process memory stays at ~1.6 GB consistently. The worst part is I will never know why. :man_shrugging: :man_facepalming: