OGeek|极客世界-中国程序员成长平台

标题: ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:05
标题: ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS

我将变量传递给另一个方法。访问此属性时,应用程序因 EXC_BAD_ACCESS 错误而崩溃。

   in class1: 
  CVPixelBufferRef pixelBuffer;
  @implementation class1
     - (void)captureOutputAVCaptureOutput *)output didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)connection {
        pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        dispatch_async( dispatch_get_main_queue(), ^{
           predictions = [dTFG runX:pixelBuffer orientation: UIDeviceOrientationPortrait CardRect: _vwRect];

       }
     }
   } 

    in class2:
    - (NSArray*) runXCVPixelBufferRef) pixelBuffer orientation: (UIDeviceOrientation) orientation CardRectCGRect) CardRect {
       CFRetain(pixelBuffer);  // Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)
    }

Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)

当我评论 dispatch_async 时,崩溃没有发生。

根据另一个答案,这个错误(可能)是因为释放了对象。但是为什么在这种情况下释放了而没有dispatch_async却没有释放。



Best Answer-推荐答案


CMSampleBufferGetImageBuffer() 返回的 CVImageBufferRef不是一个对象,它只是一个 C 结构,所以它不会参与引用计数。如果您要将其传递到异步运行的 block 中,则需要确保该数据保持有效。请注意文档具体告诉您:

The caller does not own the returned buffer, and must retain it explicitly if the caller needs to maintain a reference to it.

我没有深入研究它,但考虑到可能意味着您需要调用 CVBufferRetain() 的数据类型在 pixelBuffer 上(然后在完成后使用 CVBufferRelease())。

关于ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54982208/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4