title: iOS - CMSampleBufferRef is not being released from captureOutput:didOutputSampleBuffer:fromConnection
body: <p>I am capturing frames from the camera using the code:</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput
  didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
          :(AVCaptureConnection *)connection
  {
      // Create a UIImage from the sample buffer data
      UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
  
      if(delegate &amp;&amp; [delegate respondsToSelector:@selector(captureManagerCapturedFrame:withFrameImage:withFrameBuffer:)]) {
          [delegate captureManagerCapturedFrame:self withFrameImage:image withFrameBuffer:sampleBuffer];
      }
  
  }
  </code></pre>
  
  <p>I am doing this because in the delegate method <code>captureManagerCapturedFrame:withFrameImage:withFrameBuffer:</code> I have a flag which tells the app to use either the returned <code>uiimage</code> OR the returned <code>sampleBuffer</code>.</p>
  
  <p>The delegate method is:</p>
  
  <pre><code>- (void) captureManagerCapturedFrame:(AVCamCaptureManager *)captureManager
                        withFrameImage:(UIImage *)image
                       withFrameBuffer:(CMSampleBufferRef)frameBuffer {
  
      if(_screen1) {
          NSLog(@"Only display camera image\n");
      }
      else if(_screen2) {
          //Enable IR
          NSLog(@"Display AND Process camera image\n");
          [self imageReconigitionProcessFrame:frameBuffer];
      }
  }
  </code></pre>
  
  <p>where <code>imageReconigitionProcessFrame:</code> is:</p>
  
  <pre><code>-(void)imageReconigitionProcessFrame:(CMSampleBufferRef)frameBuffer {
  
      //CFRetain(frameBuffer);
      MSImage *qry = [[MSImage alloc] initWithBuffer:frameBuffer orientation:AVCaptureVideoOrientationPortrait]; //MEMORY LEAK HERE???
      qry = nil;
      //CFRelease(frameBuffer);
  }
  </code></pre>
  
  <p>This code effectively works. <strong>But here is my problem.</strong> When this code is run and profiled in instruments, I see a rapid increase in the <code>overall bytes</code> used, but the allocations profiler doesn't appear to increase. Nor do a see any 'leaks' using the leaks tool. But clearly, there is a rapid memory gain each time <code>imageReconigitionProcessFrame:</code> is called and the app crashes after a few seconds. When I set <code>frameBuffer</code> to <code>nil</code>, there is NO increase in memory (or course I also don't have the frame buffer to do any processing with). </p>
  
  <p>I have tried transfering ownership of <code>frameBuffer</code> using <code>CFRetain</code> and <code>CFRelease</code> (commented out in the above code), but these don't seem to do anything either.</p>
  
  <p><strong>Does anyone have any idea where I could be leaking memory inside this function???</strong>
  The method <code>[[MSImage alloc] initWithBuffer:</code> is form a third party SDK (<a href="http://www.moodstocks.com" rel="nofollow">Moodstocks</a>, which is an awesome image recognition SDK) and it works just fine in their demos, so I don't think the problem is inside this function.</p>