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.
Error: “Thread 1: EXC_BAD_ACCESS (code=1, address=0xN)” where “N” is a hypothetical hex memory address.
The 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];
});
}
This error normally occurs in the line:
CVPixelBufferLockBaseAddress(buffer, 0);
Any ideas? Thanks!
I would say the error is here:
This call is dispatched asynchronously, and to my knowledge
CMSampleBuffers don’t necessarily hold on to their contents (the data might be released before the sample buffer object).Make sure you dispatch it synchronously on the main queue.
BTW: Why do you dispatch on the main thread in the first place?