Repository: opencv
Issue: Memory leaks in invoking Imgproc HoughCircles methods.
Labels: platform: ios/osx,needs investigation,category: swift/objc bindings

### System Information

In my iOS project about using OpenCV for reading gauge meter, using `Imgproc`'s method: `+ (void)HoughCircles:(Mat*)image circles:(Mat*)circles method:(HoughModes)method dp:(double)dp minDist:(double)minDist param1:(double)param1 param2:(double)param2` to find circles. 

env:
OpenCV version: 4.6.0
Operation System / Platform: macOS 13.1 (22C65) 
Compiler: Xcode Version 14.2 (14C18)
Computer: MacBook Pro 16-inch, 2021, Apple M1 Max, 64 GB RAM, 2TB SSD

P.S.: OpenCV 4.6.0 is self-build one, not official one, cause official one not supports M1 Chip. I made a iOS xcframework for my project, the building CLI command is: 
`$ python /Users/xxxxx/workspace/opencv/4.6/opencv/platforms/apple/build_xcframework.py --out './SDK/opencv/4.6' --iphoneos_archs 'arm64' --iphonesimulator_archs 'arm64' --build_only_specified_archs --without=java --without=js --without=python --enable_nonfree  --contrib='/Users/xxxxx/workspace/opencv/4.6/opencv_contrib/' --iphoneos_deployment_target='13.0'`


### Detailed description

And simple demo app runs on the iOS device, memory runs out in seconds, it looks like this method has a defect about memory leaking. Does anyone met this problem before. I'm curious that no one has mentioned a similar problem online.

My demo app feature is simple, get a video snapshot from iPhone rear camera, and take these images to the OpenCV to let it find circles continuously. But this progress only maintains few seconds and the app crash, Xcode promots that app crashed by memory issue.

The memory view in Xcode is below:
<img width="960" alt="Screenshot 2023-01-21 at 01 17 07" src="https://user-images.githubusercontent.com/1861511/213762981-5c96bea4-16a7-48f9-9427-fc17252c6207.png">

### Steps to reproduce

The core part of my code is:

```objective-c
#import "GaugeMeterReaderWrapper.h"
#import "GaugeMeterInfoWrapper.h"
#import <opencv2/opencv2.h>
#import <MeteOCR-Swift.h>


@interface GaugeMeterReaderWrapper()
@end

@implementation GaugeMeterReaderWrapper

+ (instancetype) shared {
    static GaugeMeterReaderWrapper* _instance = NULL;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _instance = [[GaugeMeterReaderWrapper alloc] init];
    });
    return _instance;
}

- (GaugeMeterInfoWrapper *) calibrateGauge:(UIImage *)gaugeImage {
    Mat* cvGauge = [gaugeImage toCV2Mat];

    Mat* cvGaugeGray = [[Mat alloc] init];
    [Imgproc cvtColor:cvGauge dst:cvGaugeGray code:(COLOR_RGBA2GRAY)];

    Mat *circles = [[Mat alloc] init];

    // Memory leaks IN NEXT LINE. App runs without memory leaks if delete this CODE.
    [Imgproc HoughCircles:cvGaugeGray circles:circles method:HOUGH_GRADIENT dp:1 minDist:10 param1:100 param2:50]; 

    NSLog(@"Got %d circle(s)", [circles channels]);

    GaugeMeterInfoWrapper* result = [[GaugeMeterInfoWrapper alloc] init];
    result.meter = [cvGaugeGray toUIImage];
    return result;
}

@end
```

The `ViewController` here using `AVCaptureSession` to get camera captures images, and let opencv wrapper to analysis. Wrapper returns the gray image and show it on other `UIImageView`.
```swift
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate {

    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        guard let cvFrame = sampleBuffer.uiImage else { return }
        // processing image here
        refresh_cv_preview(cvFrame)
    }
}

extension ViewController {

    func refresh_cv_preview(_ cv2Img: UIImage) {
        DispatchQueue.main.async {
            let result = GaugeMeterReaderWrapper.shared().calibrateGauge(cv2Img) /// <---- call wrapper define above
            guard let meterImg = result?.meter else { return }
            self.cvPreviewView.image = meterImg
        }
    }
}

```

BTW, at the beginning, I write this mockup demo with `Swift`, met this defect too. But at time, I thought that is my code problem (forget to view the memory monitor at that time). And now, it looks like the OpenCV has the memory leaks in this `HoughCircles` methods. After delete that `HoughCircles` code line, memory runs ok.

### 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)