20
votes

I declared a property to reference a GCD queue:

@property (assign) dispatch_queue_t backgroundQueue;

In the init method of a class I create a serial queue:

backgroundQueue = dispatch_queue_create("com.company.app", DISPATCH_QUEUE_SERIAL);

ARC complains: "Assigning retained object to unsafe_unretained variable; object will be released after assignment"

Must I use __bridge_transfer?

In -dealloc I am releasing the queue:

dispatch_release(backgroundQueue);

Again, ARC complains: "ARC forbids explicit message send of 'release'"

I find this confusing because this is a C function call and thought queues are C objects for which I must take care of memory management myself! Since when does ARC handle the C-objects for me?

4

4 Answers

39
votes

In iOS 6 you can cmd+click dispatch_queue_t and see this:

/*
 * By default, dispatch 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.
 * See <os/object.h> for details.
 */

So simply use strong in the property (unless the queue is referenced elsewhere and you really want a weak reference).

Before iOS 6 you have to do memory management yourself using dispatch_retain and dispatch_release. Doing this in iOS 6 will throw a compiler error.

10
votes

This error message will come if you are using iOS 6 SDK.

In the iOS 6.0 SDK and the Mac OS X 10.8 SDK, every dispatch object is also a part of objective C. So you don't want to worry about the memory, ARC will manage the memory of dispatch_queue.

Please refer the link for details.

3
votes

You should not insist on using assign. You can use:

@property (nonatomic) dispatch_queue_t backgroundQueue;

or even

@property dispatch_queue_t backgroundQueue;

with no warning.

1
votes

The magic dispatch_retain and dispatch_release functions are defined based on lots of conditions.

Bottom line in my tests: - if you deploy for sdk 5, use them and all is well - if you deploy for sdk >= 6 , do not even think of them, just treat everything as object

You can see a better explanation here: https://stackguides.com/questions/8618632/does-arc-support-dispatch-queues?rq=1