OpenCV Error: Insufficient memory

Hello again
im having hard time finding why I am getting this. error

OpenCV Error: Insufficient memory (Failed to allocate 8294400 bytes) in OutOfMemoryError, file /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/alloc.cpp, line 52
libc++abi: terminating with uncaught exception of type cv::Exception: /Volumes/build-storage/build/master_iOS-mac/opencv/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 8294400 bytes in function OutOfMemoryError

Screen Shot 2023-03-14 at 2.38.55 PM

The thing is I have managed the memory of the. app very well its in most case a number like around 80MB

I have an array of 900 images which I am looping through them(i’m reading. them from evictee storage)

in one. part of my code I have to. get. BGRA from my images
here is the code

               OpenCVWrapper.analyze(_frame, pts: listROIPoint, pts_count: Int32(listROIPoint.count)) { first, second, third in
                        areasBGRList.append(
                            [
                                Double(round(1000000 * first) / 1000000),
                                Double(round(1000000 * second) / 1000000),
                                Double(round(1000000 * third) / 1000000)
                            ]
                        )
                        
                    }

I know the problem is somewhere in my opencvwrapper
but I can. not. find it


+ (void)analyze:(UIImage *)source pts: (NSArray<NSValue *>*)pts pts_count: (int) pts_count andCompletionHandler:(void (^)(double result0, double result1, double result2))completionHandler {
//    cout << "-> bgrFrom ->";
    std::vector<std::vector<cv::Point> > pts2;
    std::vector<cv::Point> innerpts;
    
    for (size_t i = 0; i < pts.count; i++) {
        innerpts.push_back(cv::Point(pts[i].CGPointValue.x, pts[i].CGPointValue.y));
    }
    pts2.push_back(innerpts);

    Mat bgr_image = [OpenCVWrapper _bgrFrom:[OpenCVWrapper _matFrom:source]];

    Mat tracking_mask = Mat::zeros(bgr_image.size().height, bgr_image.size().width, CV_8UC1);
    Scalar color = Scalar(255,255,255);

    
    Mat ar = Mat();
    ar.create(1, pts_count, CV_8UC1);
    fillPoly(tracking_mask, pts2, color);

    Scalar roi_mean_color = mean(bgr_image, tracking_mask);
    bgr_image.release();
    bgr_image.deallocate();
    completionHandler(roi_mean_color.val[0], roi_mean_color.val[1], roi_mean_color.val[2]);

}

#pragma mark Private
+ (Mat)_bgrFrom:(Mat)source {
//    cout << "-> bgrFrom ->";
    Mat result;
    cvtColor(source, result, COLOR_RGB2BGR);
    return result;
}
+ (Mat)_matFrom:(UIImage *)source {
//    cout << "matFrom ->";
    CGImageRef image = CGImageCreateCopy(source.CGImage);
    CGFloat cols = CGImageGetWidth(image);
    CGFloat rows = CGImageGetHeight(image);
    Mat result(rows, cols, CV_8UC4);
    
    CGBitmapInfo bitmapFlags = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault;
    size_t bitsPerComponent = 8;
    size_t bytesPerRow = result.step[0];
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image);
    CGContextRef context = CGBitmapContextCreate(result.data, cols, rows, bitsPerComponent, bytesPerRow, colorSpace, bitmapFlags);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, cols, rows), image);
    CGContextRelease(context);
    return result;
}


+ (UIImage *)_imageFrom:(Mat)source {
//    cout << "-> imageFrom\n";
    NSData *data = [NSData dataWithBytes:source.data length:source.elemSize() * source.total()];
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    CGBitmapInfo bitmapFlags = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
    size_t bitsPerComponent = 8;
    size_t bytesPerRow = source.step[0];
    CGColorSpaceRef colorSpace = (source.elemSize() == 1 ? CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB());
    CGImageRef image = CGImageCreate(source.cols, source.rows, bitsPerComponent, bitsPerComponent * source.elemSize(), bytesPerRow, colorSpace, bitmapFlags, provider, NULL, false, kCGRenderingIntentDefault);
    UIImage *result = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);
    return result;
}

im not really good with objective c so any. helps would be very appricateted why despite the fact that my memory is 80MB but I get the insufficient memory error on opencv. files