Answers:
现在长答案...
您需要在队列上使用dispatch_retain
和dispatch_release
。ARC不管理它们。
ARC将为您管理队列。您不需要(也不能)使用,dispatch_retain
或者dispatch_release
是否启用了ARC。
从iOS 6.0 SDK和Mac OS X 10.8 SDK开始,每个调度对象(包括dispatch_queue_t
)也是一个Objective-C对象。记录在<os/object.h>
头文件中:
* By default, libSystem objects such as GCD and XPC 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.
*
* NOTE: this requires explicit cancellation of dispatch sources and xpc
* connections whose handler blocks capture the source/connection object,
* resp. ensuring that such captures do not form retain cycles (e.g. by
* declaring the source as __weak).
*
* To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
* compiler flags.
*
* This mode requires a platform with the modern Objective-C runtime, the
* Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
* or iOS 6.0 deployment target.
这意味着你可以把你的队列中的NSArray
或NSDictionary
,或与一个属性strong
,weak
,unsafe_unretained
,assign
,或retain
属性。这也意味着,如果您从某个块中引用队列,该块将自动保留该队列。
因此,如果您的部署目标至少是iOS 6.0或Mac OS X 10.8,并且已启用 ARC,则ARC将保留并释放您的队列,并且编译器将标记任何尝试使用dispatch_retain
或dispatch_release
作为错误。
如果您的部署目标是至少6.0的iOS或Mac OS X 10.8,和你有ARC禁用,您必须手动保留和释放你的队列或者通过调用dispatch_retain
和dispatch_release
,或通过发送队列retain
和release
消息(像[queue retain]
和[queue release]
)。
为了与旧代码库兼容,可以通过定义OS_OBJECT_USE_OBJC
为,防止编译器将队列视为Objective-C对象0
。例如,您可以将其放在.pch
文件中(在任何#import
语句之前):
#define OS_OBJECT_USE_OBJC 0
或者您可以OS_OBJECT_USE_OBJC=0
在构建设置中添加为预处理器宏。如果设置OS_OBJECT_USE_OBJC
为0
,ARC将不会为您保留或释放您的队列,您必须使用dispatch_retain
和自己进行dispatch_release
。
dispatch_release
和 NULL
你的5.1的目标dealloc
代码。否则,某些东西(由编译器生成的代码?运行时本身?)将尝试第二次释放该对象。