简体   繁体   English

在Objective-C中的dispatch_async中传递参数时为EXC_BAD_ACCESS

[英]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.

相关问题 使用dispatch_async时为EXC_BAD_ACCESS - EXC_BAD_ACCESS when using dispatch_async 在dispatch_async中使用“freed”self时的EXC_BAD_ACCESS - EXC_BAD_ACCESS when using “freed” self in dispatch_async GCD dispatch_async DISPATCH_QUEUE_PRIORITY_DEFAULT EXC_BAD_ACCESS崩溃 - GCD dispatch_async DISPATCH_QUEUE_PRIORITY_DEFAULT EXC_BAD_ACCESS crash 使用FMDB将数据插入SQLite时,在dispatch_async上为EXC_BAD_ACCESS - EXC_BAD_ACCESS on dispatch_async while inserting data into SQLite with FMDB captureOutput 中的 Dispatch_async:(AVCaptureOutput*)captureOutput didOutputSampleBuffer,样本缓冲区上的 EXC_BAD_ACCESS 错误 - Dispatch_async in captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer, EXC_BAD_ACCESS error on sample buffer Objective-C:为什么在调用带参数的objc_msgSend时会得到EXC_BAD_ACCESS异常? - Objective-C: Why do I get an EXC_BAD_ACCESS exception when calling objc_msgSend with a parameter? Objective-C问题:EXC_BAD_ACCESS代码= 2 - Objective-C Trouble: EXC_BAD_ACCESS code=2 使用EXC_BAD_ACCESS的Objective-C回调函数错误 - objective-c callback function errors with EXC_BAD_ACCESS 调试EXC_BAD_ACCESS-Objective-C-Xcode 8.0 - Debugging a EXC_BAD_ACCESS - Objective-C - Xcode 8.0 完成处理程序上的Objective-C EXC_BAD_ACCESS - Objective-C EXC_BAD_ACCESS on Completion Handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM