Open CV drive (not released)

I use a third-party library for image processing, this method seems to be the reason for the large memory usage (+ 30 MB) every time it is executed, and it will not be released properly. Reusing this causes the application to crash (memory overload). The image used is directly connected to the camera of my iP6.

+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
    NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize() * cvMat.total()];

    CGColorSpaceRef colorSpace;

    if (cvMat.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
    }

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

    CGImageRef imageRef = CGImageCreate(cvMat.cols,                                     // Width
                                        cvMat.rows,                                     // Height
                                        8,                                              // Bits per component
                                        8 * cvMat.elemSize(),                           // Bits per pixel
                                        cvMat.step[0],                                  // Bytes per row
                                        colorSpace,                                     // Colorspace
                                        kCGImageAlphaNone | kCGBitmapByteOrderDefault,  // Bitmap info flags
                                        provider,                                       // CGDataProviderRef
                                        NULL,                                           // Decode
                                        false,                                          // Should interpolate
                                        kCGRenderingIntentDefault);                     // Intent

    // UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
    UIImage *image = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return image;
}

I suspect that the problem is here: (__bridge CFDataRef)data. I can not use CFRelease, because this can lead to a crash of the application. The project works with ARC.

EDIT:

It seems that the same code is also on the official openCV website: http://docs.opencv.org/2.4/doc/tutorials/ios/image_manipulation/image_manipulation.html

G!

2 , ( lib, ).

 cv::Mat undistorted = cv::Mat( cvSize(maxWidth,maxHeight), CV_8UC4); // here nothing
        cv::Mat original = [MMOpenCVHelper cvMatFromUIImage:_adjustedImage]; // here +30MB

        //NSLog(@"%f %f %f %f",ptBottomLeft.x,ptBottomRight.x,ptTopRight.x,ptTopLeft.x);
        cv::warpPerspective(original, undistorted,
                            cv::getPerspectiveTransform(src, dst), cvSize(maxWidth, maxHeight)); // here +16MB 



        _cropRect.hidden=YES;

        @autoreleasepool {
            _sourceImageView.image=[MMOpenCVHelper UIImageFromCVMat:undistorted]; // here +15MB (PROBLEM)
        }         

        original.release(); // here -30MB (THIS IS OK)
        undistorted.release(); // here -16MB (ok)
+4
1

, , OpenCV . , , @autoreleasepool , . , , .

, , . , , . .

0

Source: https://habr.com/ru/post/1670042/


All Articles