Does ARC support dispatch queues?
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.
Is it safe to schedule and invalidate NSTimers on a GCD serial queue? With ARC, what's better: alloc or autorelease initializers? How to properly implement ARC compatible and `alloc init` safe Singleton class? KVO and ARC how to removeObserver reusable multithread implementation in Sprite Kit EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose
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
- fake va_list in ARC
- What does “Receiver type 'CALayer' for instance message is a forward declaration” mean here?
- How do I declare an array of weak references in Swift?
- Xcode ARC (automatic reference counting), “release is unavailable”
- AVPlayer continues to play after ViewController is removed from NavigationController
- Setting uiimage to nil doesn't release memory with ARC
2 Solutions Collect From Internet About “Does ARC support dispatch queues?”
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_release on 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_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.
This means you can store your queue in an
NSDictionary, or in a property with one of the
retain attributes. 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_release as 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_release, or by sending the queue
release messages (like
[queue retain] and
For compatibility with old codebases, you can prevent the compiler from seeing your queue as an Objective-C object by defining
0. For example, you can put this in your
.pch file (before any
#define OS_OBJECT_USE_OBJC 0
or you can add
OS_OBJECT_USE_OBJC=0 as a preprocessor macro in your build settings. If you set
0, ARC will not retain or release your queue for you, and you will have to do it yourself using
Just a follow up here… If your minimum deployment target is iOS 6, ARC now manages them.
- Does Xcode have a watch window?
- Writing and reading text files on the iPhone
- How to create Entitlement.plist file in xcode 5?
- Xcode 7.0.1 with CocaoPods app compile error Exit code 1
- how to say “same class” in a Swift generic
- Swift startsWith method?
- Adding a pin annotation to a map view on a long press in swift
- How can I guarantee unique entries in a Core Data store in a shared app container used by both the host app and an extension?
- Xcode 5 – remove source control for project
- UIImagePickerController's cameraViewTransform is ignoring 'scaling' and 'translation' on iOS 10 beta
- Create UIImage from shadowed view while retaining alpha?
- iOS: Using UIAppearance to define custom UITableViewCell color
- Index '5' beyond bounds of empty array crash
- iOS – How to check if an NSOperation is in an NSOperationQueue?
- Use NSNotificationCenter selector for one observer in multiple view controllers