您的位置:首页 > 其它

ARC开启的情况下是否需要对GCD对象dispatch_release ?

2014-04-04 13:05 453 查看
If your deployment target is iOS 6.0 or Mac OS X 10.8 or later

ARC will manage your queue for you. You do not need to (and cannot) use dispatch_retain or dispatch_release if ARC is enabled.

Starting in the iOS 6.0 SDK and the Mac OS X 10.8 SDK, every dispatch object (including a dispatch_queue_t) is also an Objective-C object. This is documented in the <os/object.h> header file:

* 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.


在dealloc方法中应该加上

#if !OS_OBJECT_USE_OBJC //这个宏是在sdk6.0之后才有的,如果是之前的,则OS_OBJECT_USE_OBJC为0

dispatch_release(_barrierQueue);

#endif

当然了,也可以使用

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000 // 6.0sdk之前

dispatch_release(_barrierQueue);

#endif

这里的宏 __IPHONE_OS_VERSION_MIN_REQUIRED 就是我们在工程的设置项里设置的最低部署sdk版本



原因就是 对于最低sdk版本>=ios6.0来说,GCD对象已经纳入了ARC的管理范围,我们就不需要再手工调用 dispatch_release了,否则的话,在sdk<6.0的时候,即使我们开启了ARC,这个宏OS_OBJECT_USE_OBJC 也是没有的,也就是说这个时候,GCD对象还必须得自己管理

如果你部署的最低目标低于 iOS 6.0 or Mac OS X 10.8

你应该自己管理GCD对象,使用(dispatch_retain,dispatch_release),ARC并不会去管理它们

如果你部署的最低目标是 iOS 6.0 or Mac OS X 10.8 或者更高的

ARC已经能够管理GCD对象了,这时候,GCD对象就如同普通的OC对象一样,不应该使用dispatch_retain ordispatch_release
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: