简体   繁体   English

iPhone如何使用ARC正确处理Core Foundation引用?

[英]iPhone how to properly handle Core Foundation references with ARC?

I'm very new to the core foundation programming and would like to know what I'm thinking of doing is correct. 我对核心基础编程非常陌生,想知道我在想什么是正确的。 I'm using ARC, and am not sure of how it handles non-object references. 我正在使用ARC,并且不确定它如何处理非对象引用。 I need to save a reference to a sample buffer and use it later within the app. 我需要保存对示例缓冲区的引用,稍后在应用程序中使用它。 Is this possible, or will the sample buffer be deallocated prior to that? 这是可能的,还是样本缓冲区会在此之前解除分配?

Will using self.sampleBuffer = sampleBuffer_; 将使用self.sampleBuffer = sampleBuffer_; cause memory leak? 导致内存泄漏? Do I need to add a manual call to release after this call? 此次通话后是否需要添加手动呼叫才能发布?

@property (nonatomic)CMSampleBufferRef sampleBuffer;

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer_ fromConnection:(AVCaptureConnection *)connection
{
    //does this cause memory leak?
    self.sampleBuffer = sampleBuffer_;
}

It doesn't cause a memory leak. 它不会导致内存泄漏。 In fact, you're more likely to run into issues from the object being released out from under you, since properties have the assign attribute by default, which means they do not retain (read: own) the assigned object. 实际上,您更有可能遇到从您下面释放的对象的问题,因为默认情况下属性具有assign属性,这意味着它们不保留(读取:拥有)已分配的对象。

If you're holding onto the sample buffer long enough to need it to be a property, you should probably follow the docs and copy the samples to your own buffer instead of holding onto the object handed to your delegate: 如果您持有足够长的样本缓冲区以使其成为属性,则应该按照文档将样本复制到您自己的缓冲区,而不是保留传递给您的委托的对象:

If your application is causing samples to be dropped by retaining the provided CMSampleBuffer objects for too long, but it needs access to the sample data for a long period of time, consider copying the data into a new buffer and then releasing the sample buffer (if it was previously retained) so that the memory it references can be reused. 如果您的应用程序通过保留提供的CMSampleBuffer对象太长时间而导致删除样本,但它需要长时间访问样本数据,请考虑将数据复制到新缓冲区中,然后释放样本缓冲区(如果它以前保留过)以便它引用的内存可以重用。

On the other hand, if you really want to keep a reference to the sample buffer, you can keep everything else as it is and call CFRetain on the object before you assign it to your property (making sure you CFRelease the object that was assigned before). 另一方面,如果你真的想保留对样本缓冲区的引用,你可以保留其他所有内容,并在将它分配给你的属性之前在对象上调用CFRetain (确保你CFRelease之前分配的对象) )。 This has equivalent semantics to the strong attribute for Foundation objects. 这与Foundation对象的strong属性具有等效的语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM