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

**URL:** <https://forum.opencv.org/t/user-error-of-cuda-templatematching-or-actual-memory-leak/14243>\
**Category:** C++\
**Tags:** cuda\
**Created:** [August 14, 2023, 6:54am UTC](https://forum.opencv.org/t/user-error-of-cuda-templatematching-or-actual-memory-leak/14243 "2023-08-14T06:54:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rukerin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rukerin/32/6326_2.png) [@Rukerin](https://forum.opencv.org/u/Rukerin)\
**Post date:** [August 14, 2023, 6:54am UTC](https://forum.opencv.org/t/user-error-of-cuda-templatematching-or-actual-memory-leak/14243/1 "2023-08-14T06:54:24Z")

</div>

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!

```auto
#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;
}

```

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [August 14, 2023, 8:02am UTC](https://forum.opencv.org/t/user-error-of-cuda-templatematching-or-actual-memory-leak/14243/2 "2023-08-14T08:02:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rukerin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rukerin/32/6326_2.png) [@Rukerin](https://forum.opencv.org/u/Rukerin)\
**Post date:** [August 14, 2023, 3:45pm UTC](https://forum.opencv.org/t/user-error-of-cuda-templatematching-or-actual-memory-leak/14243/3 "2023-08-14T15:45:58Z")

</div>

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. 🤷‍♂️ 🤦‍♂️
