title: EXC_BAD_ACCESS in a random time after start a task
body: <p>I have a piece of code of my application that's I get the error where I don't have idea what's the problem and this error occurs when I open the device camera and start to get capture frames. The weird thing here is, this works but gets the error below in a short or long time of the capture has started.</p>
  
  <blockquote>
    <p>Error: "Thread 1: EXC_BAD_ACCESS (code=1, address=0xN)" where "N" is a hypothetical hex memory address.</p>
  </blockquote>
  
  <p>The code:</p>
  
  <pre><code>- (void)imageToBuffer:(CMSampleBufferRef)source
  { 
    NSData *data;
  
    CVImageBufferRef buffer = CMSampleBufferGetImageBuffer(source);
    CVPixelBufferLockBaseAddress(buffer, 0);
  
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(buffer);
    size_t height = CVPixelBufferGetHeight(buffer);
    void *bufferSrc = CVPixelBufferGetBaseAddress(buffer);
  
    data = [NSData dataWithBytes:bufferSrc length:bytesPerRow * height];
    CVPixelBufferUnlockBaseAddress(buffer, 0);
  
    [self.delegate didReceivedFrame:data];
  }
  
  @end
  
  
  #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate implementation
  @implementation AVCaptureManager (AVCaptureVideoDataOutputSampleBufferDelegate)
  
  - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  {
    dispatch_async(dispatch_get_main_queue(), ^{
      [self imageToBuffer:sampleBuffer];
    });
  }
  </code></pre>
  
  <p>This error normally occurs in the line:</p>
  
  <pre><code>CVPixelBufferLockBaseAddress(buffer, 0);
  </code></pre>
  
  <p>Any ideas? Thanks!</p>