title: CGBitmapContextCreateImage - vm_copy failed - iPhone SDK
body: <p>I am having an issue using CGBitmapContextCreateImage in my iPhone app.</p>
  
  <p>I am using AV Foundation Framework to grab camera frames using this method:</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
      CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
      CVPixelBufferLockBaseAddress(imageBuffer,0);
      uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
      size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
      size_t width = CVPixelBufferGetWidth(imageBuffer);
      size_t height = CVPixelBufferGetHeight(imageBuffer);
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
      CGImageRef newImage = CGBitmapContextCreateImage(newContext);
      CVPixelBufferUnlockBaseAddress(imageBuffer,0);
      CGContextRelease(newContext);
      CGColorSpaceRelease(colorSpace);
  
      UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
      self.imageView.image = image;
  
      CGImageRelease(newImage);
  
  } 
  </code></pre>
  
  <p>However, I am seeing an error in the debug console as its runs:</p>
  
  <pre><code>&lt;Error&gt;: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.
  </code></pre>
  
  <p>Has anyone ever seen this? By commenting out lines I have narrowed the problem line out to:</p>
  
  <pre><code>CGImageRef newImage = CGBitmapContextCreateImage(newContext);
  </code></pre>
  
  <p>but I am not sure how to get rid of it.  Functionally, it works great.  So clearly, the CGImage is being created, but I need to know what is causing the error so it doesn't affect other parts.</p>
  
  <p>Many thanks. Any help/advice would be great!
  Brett</p>