Search code examples
iosgrand-central-dispatchdispatch-async

Do I need to release the dispatch_queue_t created by dispatch_queue_create


Developing an iOS application with ARC enabled, and I need to an instance of dispatch_queue_t to carry on some background task.

Wonder do I need to call dispatch_release for the dispatch_queue_t created by dispatch_queue_create within an ARC environment?

Thanks,


Solution

  • In short, no, you don’t call dispatch_release when using ARC.

    The documentation says:

    Important

    If your app is built with a deployment target of macOS 10.8 and later or iOS v6.0 and later, dispatch queues are typically managed by ARC, so you do not need to retain or release the dispatch queues.

    If you try calling dispatch_release, it will report an error which makes this very clear:

    'release' is unavailable: not available in automatic reference counting mode


    FWIW, while one wouldn’t generally do this nowadays, it’s worth noting that one can opt out of ARC management of GCD objects by defining OS_OBJECT_USE_OBJC to be 0 (e.g. a build setting that sets the C flags with -DOS_OBJECT_USE_OBJC=0). If you opt out of ARC management of GCD objects, then you do would need dispatch_release (and all the concomitant challenges that manual management of GCD objects entails). Otherwise, ARC projects would not use dispatch_release.

    If you’re dealing with a contemporary project, this is not a practical concern. But if you’re dealing with older projects or source that might be included in legacy projects, then you might want to check (with #if) for OS_OBJECT_USE_OBJC and make a decision from that.