Ios – Why is ARC complaining about dispatch_queue_create and dispatch_release in iOS 6

automatic-ref-countinggrand-central-dispatchiosmemory-management

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?

Best Answer

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.