ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS
<p><p>我将变量传递给另一个方法。访问此属性时,应用程序因 EXC_BAD_ACCESS 错误而崩溃。</p>
<pre><code> 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 = ;
}
}
}
in class2:
- (NSArray*) runX:(CVPixelBufferRef) pixelBuffer orientation: (UIDeviceOrientation) orientation CardRect:(CGRect) CardRect {
CFRetain(pixelBuffer);// Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)
}
</code></pre>
<blockquote>
<p>Thread 1: EXC_BAD_ACCESS (code=1, address=0x800000060)</p>
</blockquote>
<p>当我评论 dispatch_async 时,崩溃没有发生。</p>
<p>根据另一个答案,这个错误(可能)是因为释放了对象。但是为什么在这种情况下释放了而没有dispatch_async却没有释放。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><a href="https://developer.apple.com/documentation/coremedia/1489236-cmsamplebuffergetimagebuffer?language=objc" rel="noreferrer noopener nofollow"><code>CMSampleBufferGetImageBuffer()</code></a> 返回的 <code>CVImageBufferRef</code>不是一个对象,它只是一个 C 结构,所以它不会参与引用计数。如果您要将其传递到异步运行的 block 中,则需要确保该数据保持有效。请注意文档具体告诉您:</p>
<blockquote>
<p><em>The caller does not own the returned buffer, and must retain it explicitly if the caller needs to maintain a reference to it.</em></p>
</blockquote>
<p>我没有深入研究它,但考虑到可能意味着您需要调用 <a href="https://developer.apple.com/documentation/corevideo/1535810-cvbufferretain?language=objc" rel="noreferrer noopener nofollow"><code>CVBufferRetain()</code></a> 的数据类型在 <code>pixelBuffer</code> 上(然后在完成后使用 <code>CVBufferRelease()</code>)。</p></p>
<p style="font-size: 20px;">关于ios - 在 objective-C 的 dispatch_async 中传递参数时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/54982208/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/54982208/
</a>
</p>
页:
[1]