为什么ARC抱怨iOS 6中的dispatch_queue_create和dispatch_release?

ope*_*rog 19 memory-management grand-central-dispatch ios automatic-ref-counting

我声明了一个属性来引用GCD队列:

@property (assign) dispatch_queue_t backgroundQueue;
Run Code Online (Sandbox Code Playgroud)

在类的init方法中,我创建了一个串行队列:

backgroundQueue = dispatch_queue_create("com.company.app", DISPATCH_QUEUE_SERIAL);
Run Code Online (Sandbox Code Playgroud)

ARC抱怨:"将保留对象分配给unsafe_unretained变量;对象将在分配后释放"

我必须使用__bridge_transfer吗?

在-dealloc我发布队列:

dispatch_release(backgroundQueue);
Run Code Online (Sandbox Code Playgroud)

ARC再次提出抱怨:"ARC禁止发布'发布'的明确消息"

我发现这令人困惑,因为这是一个C函数调用,思想队列是C对象,我必须自己处理内存管理!从什么时候开始为我处理C-objects?

Jan*_*ano 39

在iOS 6中,您可以cmd +单击dispatch_queue_t并查看:

/*
 * By default, dispatch objects are declared as Objective-C types when building
 * with an Objective-C compiler. This allows them to participate in ARC, in RR
 * management by the Blocks runtime and in leaks checking by the static
 * analyzer, and enables them to be added to Cocoa collections.
 * See <os/object.h> for details.
 */
Run Code Online (Sandbox Code Playgroud)

因此,只需在属性中使用strong(除非在其他地方引用队列,并且您确实需要弱引用).

在iOS 6之前,您必须使用dispatch_retain和dispatch_release自行进行内存管理.在iOS 6中执行此操作将引发编译器错误.

  • 所以...我在iOS 5中的代码100%正确...现在是不正确的内存管理?真棒!另外:很棒的编译器消息(叹气). (6认同)

Mid*_* MP 9

如果您使用的是iOS 6 SDK,则会出现此错误消息.

iOS 6.0 SDKMac OS X 10.8 SDK,每个调度对象也是目标C的一部分.所以你不要担心内存,ARC会管理内存dispatch_queue.

请参阅链接了解详情.