在ARC中使用__attribute __((NSObject))的正确方法? [英] Right way to use __attribute__((NSObject)) with ARC?

查看:357
本文介绍了在ARC中使用__attribute __((NSObject))的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅以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 retains 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆