0
我有我的應用程序的一段代碼,我得到的錯誤,我不知道什麼是問題,當我打開設備相機,並開始得到這個錯誤捕獲幀。這裏奇怪的是,這個工作正常,但是在捕獲已經開始的很短或者很長的時間內出現下面的錯誤。EXC_BAD_ACCESS在啓動任務後的隨機時間
Error: "Thread 1: EXC_BAD_ACCESS (code=1, address=0xN)" where "N" is a hypothetical hex memory address.
代碼:
- (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];
});
}
這個錯誤通常發生在該行:
CVPixelBufferLockBaseAddress(buffer, 0);
任何想法?謝謝!
謝謝埃裏克,工作得很好。關於在主線程中運行此進程。我得到的相機幀通過WiFi或藍牙網絡傳遞。可能我需要優化這個,我不確定這是否會順利使用主線程。 – 2012-04-03 03:41:05
我有同樣的隨機崩潰。我需要獲取當前的CMSampleBufferRef並在以後隨時使用它。我應該只使用AVCaptureVideoDataOutputSampleBufferDelegate方法中的dispatch_sync來複制當前緩衝區嗎?我應該使用CMSampleBufferCreateCopy嗎? – 2017-02-19 09:05:43