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

It might be a dumb question, but does ARC (automatic link counting) interact with Open CV when programming in iOS?

I ask about this because I recently started using Open CV in general. Although on iOS with ARC there is no need to worry about objects, since I only need to make sure that they do not have pointers pointing to them so that they can be freed.

An open CV looks like a similar cleanup function:

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

When using the new interface, most of the freeing up memory and even memory allocation operations are performed automatically when necessary.

First, does this mean that I do not need to release these objects?

Secondly, I use OpenCV and Objective-C in the same .mm file. Can we assume that the objects will not mess around with each other?

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

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

What 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.

+4
source share
1 answer

First, does this mean that I do not need to release these objects?

Yes, until you create them using the new operator. Take a 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. 

Secondly, I use OpenCV and Objective-C in the same .mm file. Can we assume that the objects will not mess around with each other?

In the initWithCVMat function, image data is copied to the NSData strong> object, which is used to initialize a new instance of UIImage .

For you, this means:
1) An instance of the UIImage class will be responsible for the release of image data. If you use ARC, this will be done automatically; otherwise, you must invoke the [release of UIImage] .
2) Do not worry about cv :: Mat - it will automatically free memory upon destruction.

+4
source

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


All Articles