保留核心基金会财产 [英] Retained Core Foundation Property
问题描述
(Xcode 4.2,iOS 5, ARC )
(Xcode 4.2, iOS 5, ARC)
我有一些属性应该取得所有权的Core Foundation(/ Graphics)对象他们的对象。现在在这些 Apple docs 我发现了这个:
I have some properties of Core Foundation (/Graphics) objects that should take ownership of their objects. Now in these Apple docs I found this:
在OS X v10.6及更高版本中,你可以使用__attribute__关键字指定应将Core Foundation属性视为
用于内存管理的Objective-C对象:
@property(retain)__ attribute __((NSObject))CFDictionaryRef myDictionary ;
不幸的是我找不到任何详细说明。我正在使用它:
Unfortunately I couldn't find any elaboration on this. I'm using this:
@property(nonatomic,strong)__ attribute __((NSObject))CGImageRef loupeImage;
它看起来像我期望的那样。它保留了设置属性的对象,并在我将属性设置为nil时将其释放。
And it seems to work the way I'd expect. It retains the object on setting the property and releases it when I set the property to nil.
现在我的问题是,我是否还需要将这些属性显式设置为nil在我的dealloc?
Now my question is, do I still need to explicitly set those properties to nil in my dealloc?
推荐答案
编辑:我以前的帖子不正确。属性上的 __ attribute __((NSObject))
仅在使用属性访问器时使其保留/释放属性。它不会影响对ivar的访问,特别是它不会在 dealloc
中删除(或释放)该属性。所以,是的,在您的 dealloc
中,您需要说 self.loupeImage = nil;
或者您需要说 [_ loupeImage release]
。
My previous post was incorrect. __attribute__((NSObject))
on the property only causes it to retain/release the property when using the property accessors. It does not affect access to the ivar, and notably, it does not nil out (or release) the property in dealloc
. So yes, in your dealloc
you either need to say self.loupeImage = nil;
or you need to say [_loupeImage release]
.
原帖:
如果编译器接受 strong
关键字,那么它将正确处理它,保留并释放为预期,并在ARC中自动终止。 __ attribute __((NSObject))
的整个想法是告诉编译器将此对象完全视为obj-c对象,这就是它的作用。
If the compiler accepts the strong
keyword, then it's going to treat it correctly, retaining and releasing as expected, and nilling out automatically in ARC. The whole idea of __attribute__((NSObject))
is it tells the compiler to treat this object exactly as if it were an obj-c object, and so that's what it does.
所以不,你不应该在dealloc中明确地将它们取出来。您将自动获得nilling / release的默认ARC行为。
So no, you shouldn't have to explicitly nil them out in dealloc. You'll get the default ARC behavior of nilling/releasing automatically.
这篇关于保留核心基金会财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!