[英]EXC_BAD_ACCESS when passing a parameter in dispatch_async in objective-C
I pass a variable to another method. 我将变量传递给另一种方法。 when accessing this property app crashes with EXC_BAD_ACCESS error.
访问此属性时,应用程序崩溃,并显示EXC_BAD_ACCESS错误。
in class1:
CVPixelBufferRef pixelBuffer;
@implementation class1
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
dispatch_async( dispatch_get_main_queue(), ^{
predictions = [dTFG runX:pixelBuffer orientation: UIDeviceOrientationPortrait CardRect: _vwRect];
}
}
}
in class2:
- (NSArray*) runX:(CVPixelBufferRef) pixelBuffer orientation: (UIDeviceOrientation) orientation CardRect:(CGRect) CardRect {
CFRetain(pixelBuffer); // Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)
}
Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x800000060)
When I commented dispatch_async, crash not happened. 当我评论dispatch_async时,没有发生崩溃。
According to another answer, this error is (probably) because of released object. 根据另一个答案,此错误是(可能)是由于释放对象引起的。 but why is it released in this situation but without dispatch_async, it is not released.
但是为什么在这种情况下将其释放,但没有dispatch_async,则不会释放它。
The CVImageBufferRef
returned by CMSampleBufferGetImageBuffer()
isn't an object, it's just a C structure, so it won't participate in reference counting. CMSampleBufferGetImageBuffer()
返回的CVImageBufferRef
不是对象,它只是一个C结构,因此它不参与引用计数。 If you're going to pass it into a block that runs asynchronously, you need to ensure that that data remains valid. 如果要将其传递到异步运行的块中,则需要确保数据仍然有效。 Notice that the documentation tells you specifically:
请注意,文档特别告诉您:
The caller does not own the returned buffer, and must retain it explicitly if the caller needs to maintain a reference to it.
调用者不拥有返回的缓冲区,如果调用者需要维护对其的引用,则必须明确保留它。
I haven't dug too far into it, but given the data type that probably means that you need to call CVBufferRetain()
on pixelBuffer
(and then CVBufferRelease()
when you're done with it). 我已经不太远了进去挖,但考虑到数据类型,可能意味着你需要调用
CVBufferRetain()
上pixelBuffer
(然后CVBufferRelease()
当你用它做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.