在ARC中使用__attribute __((NSObject))的正确方法? [英] Right way to use __attribute__((NSObject)) with ARC?
问题描述
我仅以CFNumber为例,所以可以是任何类型的没有Fundation免费电话的部分!
我只是写一些这样的测试代码:
I just write some test code like this:
typedef __attribute__((NSObject)) CFNumberRef MYNumberRef;
int main(int argc, const char * argv[])
{
@autoreleasepool {
MYNumberRef ptr = NULL;
double myDouble = 10.1;
ptr = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &myDouble);
CFIndex count = CFGetRetainCount(ptr);
}
return 0;
}
计数为2很奇怪,但是如果我使用CFNumberRef
,则计数为1.看来arc
并未考虑CFType
名称约定,只是retain
s是返回值.
It is very strange that the count is 2. But if I use CFNumberRef
, the count is 1. It seems the arc
don't take the CFType
name convention into account, it just retain
s the return value.
因此,如果我使用__attribute__((NSObject))
声明CFType
属性. 这篇文章说you shouldn't have to explicitly nil them out in dealloc.
,但是如果我这样使用:
So if I use the __attribute__((NSObject))
to declare CFType
property. This post said you shouldn't have to explicitly nil them out in dealloc.
But if I use like this:
@property (strong, nonatomic, readwrite) __attribute__((NSObject)) CFNumberRef number;
然后:
self.number = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &myDouble);
如果不使用dealloc
方法将其释放,则不会发生内存泄漏?也许我应该这样使用它:
There is no memory leak if I don't release it in the dealloc
method? Maybe I should use it like this:
CFNumbeRef ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &myDouble);
self.number = ref;
CFRelease(ref);
苹果对此有话要说吗?
推荐答案
请勿执行此操作.
在 Clang文档中,Apple确实对此有话要说:
Apple does have something to say about it in the Clang documentation:
不建议使用
__attribute__((NSObject))
typedefs.如果绝对需要使用此属性,请非常明确地使用typedef,并且不要假设__typeof
和C ++模板参数替换之类的语言功能会保留该属性.
The use of
__attribute__((NSObject))
typedefs is not recommended. If it’s absolutely necessary to use this attribute, be very explicit about using the typedef, and do not assume that it will be preserved by language features like__typeof
and C++ template argument substitution.
CFGetRetainCount
是毫无意义.比无意义更糟糕,因为您认为这可能意味着某些事情.
CFGetRetainCount
is meaningless. Worse than meaningless because you think it might mean something.
这篇关于在ARC中使用__attribute __((NSObject))的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!