Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ARC complaining about dispatch_queue_create and dispatch_release in iOS 6?

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?

like image 470
openfrog Avatar asked Dec 04 '12 12:12

openfrog


4 Answers

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.

like image 154
Jano Avatar answered Oct 25 '22 06:10

Jano


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.

like image 37
Midhun MP Avatar answered Oct 25 '22 07:10

Midhun MP


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.

like image 3
Dan Rosenstark Avatar answered Oct 25 '22 06:10

Dan Rosenstark


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://stackoverflow.com/questions/8618632/does-arc-support-dispatch-queues?rq=1

like image 1
Mihai Timar Avatar answered Oct 25 '22 07:10

Mihai Timar