title: Open-CV memory management in iOS (Objective-c with ARC)
body: <p>This might be a silly question but does ARC (automatic reference counting) interact with Open CV when programming in the iOS?</p>
  
  <p>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. </p>
  
  <p>Open CV seems to have a similar cleaning function:</p>
  
  <p><a href="http://opencv.willowgarage.com/documentation/cpp/memory_management.html" rel="nofollow">http://opencv.willowgarage.com/documentation/cpp/memory_management.html</a></p>
  
  <blockquote>
    <p>When using the new interface, the most of memory deallocation and even
    memory allocation operations are done automatically when needed.</p>
  </blockquote>
  
  <p>First, does this means I don't have to release those objects?</p>
  
  <p>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?</p>
  
  <p>For example this will give me an UIImage from a cv Mat:</p>
  
  <pre><code>+ (UIImage *)imageWithCVMat:(const cv::Mat&amp;)cvMat
  {
      return [[UIImage alloc] initWithCVMat:cvMat];
  }
  </code></pre>
  
  <p>Which uses this:</p>
  
  <pre><code>- (id)initWithCVMat:(const cv::Mat&amp;)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;
  }
  </code></pre>
  
  <p>*method taken from Robin Summerhill</p>
  
  <p>Thanks in advance for any advice.</p>