I’m reading apple’s documentation about “Memory Management for Dispatch Queues”:
Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory.
I know that ARC is not a garbage collector but I’d like to be sure that I don’t need to dispatch_retain and dispatch_release my dispatch_queue_t
The short answer: YES, ARC retains and releases dispatch queues.
And now for the long answer…
If your deployment target is lower than iOS 6.0 or Mac OS X 10.8
You need to use
dispatch_retainanddispatch_releaseon your queue. ARC does not manage them.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_retainordispatch_releaseif ARC is enabled.Details
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:This means you can store your queue in an
NSArrayorNSDictionary, or in a property with one of thestrong,weak,unsafe_unretained,assign, orretainattributes. It also means that if you refer to your queue from a block, the block will retain the queue automatically.So if your deployment target is at least iOS 6.0 or Mac OS X 10.8, and you have ARC enabled, ARC will retain and release your queue, and the compiler will flag any attempt to use
dispatch_retainordispatch_releaseas an error.If your deployment target is at least iOS 6.0 or Mac OS X 10.8, and you have ARC disabled, you must manually retain and release your queue, either by calling
dispatch_retainanddispatch_release, or by sending the queueretainandreleasemessages (like[queue retain]and[queue release]).For compatibility with old codebases, you can prevent the compiler from seeing your queue as an Objective-C object by defining
OS_OBJECT_USE_OBJCto0. For example, you can put this in your.pchfile (before any#importstatements):or you can add
OS_OBJECT_USE_OBJC=0as a preprocessor macro in your build settings. If you setOS_OBJECT_USE_OBJCto0, ARC will not retain or release your queue for you, and you will have to do it yourself usingdispatch_retainanddispatch_release.