2012-04-02 27 views
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); 

任何想法?謝謝!

回答

1

我想說的錯誤是在這裏:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self imageToBuffer:sampleBuffer]; 
}); 

該調用是異步分派,據我所知,CMSampleBuffer■不要一定堅持自己的內容(數據可能樣本緩衝區之前發佈目的)。

確保您在主隊列上同步發送

順便說一句:爲什麼你在第一個主要線程派遣?

+0

謝謝埃裏克,工作得很好。關於在主線程中運行此進程。我得到的相機幀通過WiFi或藍牙網絡傳遞。可能我需要優化這個,我不確定這是否會順利使用主線程。 – 2012-04-03 03:41:05

+0

我有同樣的隨機崩潰。我需要獲取當前的CMSampleBufferRef並在以後隨時使用它。我應該只使用AVCaptureVideoDataOutputSampleBufferDelegate方法中的dispatch_sync來複制當前緩衝區嗎?我應該使用CMSampleBufferCreateCopy嗎? – 2017-02-19 09:05:43

相關問題