Repository: opencv
Issue: Memory Leak with CascadeClassifier
Labels: bug,category: objdetect

### System Information

<Environment 1>
OpenCV version: 4.8.0
OS: Ubuntu 20.04.6 LTS
Compiler: GCC 9.4.0

<Environment 2>
OpenCV version: 4.5.3
OS: Debian 10.13
Compiler: GCC 8.3.0

### Detailed description

Maybe, this issue is related to https://github.com/opencv/opencv/issues/11559, which have not been solved.
I reproduced and measured the memory leak of CascadeClassifier by a different way on C++.

I wrote the following codes, and measure its memory usage. 
```
cv::CascadeClassifier detector("haarcascade_frontalface_default.xml");
std::vector<cv::Mat> mat_list;
for (int i=0; i<30000; i++){
	detector.detectMultiScale(pos_img, faces);
	cv::Mat tmp_img(240,120,CV_8UC1);
	mat_list.push_back(tmp_img);
}
mat_list.clear();
```
Memory usage increased inside the for loop, and I expected memory to be released when I exited the for loop and executed "mat_list.clear()", but that did not happen!
When I commented out "detector.detectMultiScale()", memory was released as I expected.

### Steps to reproduce

Please build & run following codes, and view "mem.csv" file.  
You can see the graph like below.
<img width="241" alt="graph2" src="https://github.com/opencv/opencv/assets/1311670/8a68c384-da7d-4576-8071-c8fcc27c66be">

If you comment out "detector.detectMuiltiScale()",  the graph is like below:
<img width="241" alt="graph1" src="https://github.com/opencv/opencv/assets/1311670/c3df0e8c-b42b-4163-92a6-c7b0b10aef4d">

With any image file or trained cascade file, you can reproduce the issue.

```
#include <opencv2/imgcodecs.hpp>
#include <opencv2/objdetect.hpp>
#include <sys/sysinfo.h>
#include <iostream>


int main(int argc, char * argv[])
{
    std::string face_img = "lena.jpg";
    std::string cascade_file = "haarcascade_frontalface_default.xml";

	// Load Face Image
    cv::Mat pos_img = cv::imread(face_img);
	if(pos_img.empty()){
		printf("Fail to load image %s\n", face_img.c_str());
	}

	// Load trained classifier file
	cv::CascadeClassifier detector;
	if(!detector.load(cascade_file)){
		printf("Fail to load %s\n", cascade_file.c_str());
		return -1;
	};

	// write memory usage to mem_c.csv
    FILE *f = fopen("mem_c.csv", "w");

	// Repeat to measure memory leak
	std::vector<cv::Mat> mat_list;	// Redundant cv::Mat storage
    for(int counter = 0; counter < 7 * 3600 * 1; counter++){
		std::vector<cv::Rect> faces;
		// Detect Face from an image.
		detector.detectMultiScale(pos_img, faces);
		//printf("detect %d face\n", faces.size());

		// Just for consuming memory
		cv::Mat tmp_img(240,120,CV_8UC1);
		mat_list.push_back(tmp_img);

		// Write memory usage into file
        if(counter % 40 == 0){
            struct sysinfo info;
            sysinfo(&info);
            long long virtualMemUsed = info.totalram - info.freeram;
            virtualMemUsed *= info.mem_unit;
            fprintf(f, "%lld\n", virtualMemUsed);
            fflush(f);
        }
    }
	// Release memory
	mat_list.clear();

	// Repeat to detect face, without storing redundant cv::Mat()s
    for(int counter=0; counter < 7 * 360 * 1; counter++){
		std::vector<cv::Rect> faces;
		detector.detectMultiScale(pos_img, faces, 1.3, 3, 0, cv::Size(200, 100), cv::Size(400,200));
		//printf("detect %d face\n", faces.size());

        if(counter % 40 == 0){
            struct sysinfo info;
            sysinfo(&info);
            long long virtualMemUsed = info.totalram - info.freeram;
            virtualMemUsed *= info.mem_unit;
            fprintf(f, "%lld\n", virtualMemUsed);
            fflush(f);
        }
    }
    fclose(f);
}
```

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)