简体   繁体   中英

Open-CV memory management in iOS (Objective-c with ARC)

This might be a silly question but does ARC (automatic reference counting) interact with Open CV when programming in the iOS?

I am asking this because I just recently started using Open CV in general. While on iOS with ARC it is not necessary to worry about objects since I only have to make sure to have no pointers pointing at them so that they can be released.

Open CV seems to have a similar cleaning function:

http://opencv.willowgarage.com/documentation/cpp/memory_management.html

When using the new interface, the most of memory deallocation and even memory allocation operations are done automatically when needed.

First, does this means I don't have to release those objects?

Second, I am using both OpenCV and Objective C in the same .mm file. Is it safe to assume that the objects won't be messing with each other?

For example this will give me an UIImage from a cv Mat:

+ (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat
{
    return [[UIImage alloc] initWithCVMat:cvMat];
}

Which uses this:

- (id)initWithCVMat:(const 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   

    self = [self initWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return self;
}

*method taken from Robin Summerhill

Thanks in advance for any advice.

First, does this means I don't have to release those objects?

Yes, until you create them using new operator. Look at this code:

{
cv::Mat image(640,480); // allocate image
//.. do something with it
self.myImage = [UIImage imageWithCVMat:image];
}
// Out of scope - image is deallocated automatically.
// But self.myImage is still accessible since it contains a copy of image.

Second, I am using both OpenCV and Objective C in the same .mm file. Is it safe to assume that the objects won't be messing with each other?

In your initWithCVMat functiton the image data is copied to NSData object which is used to initialize a new instance of UIImage .

For you it means:
1) An instance of UIImage class will be responsible of releasing image data. If you use ARC - it will be done automatically; otherwise you have to call [UIImage release] .
2) Don't worry about cv::Mat - it will release memory automatically when destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM