Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Auto-ARC Conversion: Assigning retained object to unsafe property; object will be released after assignment

I just converted an old project to ARC using Xcode's automatic refactoring.

@property (nonatomic, retain) NSMutableArray *cards;

was replaced by:

@property (nonatomic) NSMutableArray *cards;

This makes sense because what I've read is that "strong" is the default state. However, the following line is giving me the error in the title:

self.cards = [[NSMutableArray alloc] initWithCapacity:54];

The error is solved by adding strong back in where retain used to be:

@property (nonatomic, strong) NSMutableArray *cards;

However... if I need to go back and put strong in to every @property declaration that was retain... why did the ARC refactoring remove them all?

like image 210
Kenny Wyland Avatar asked Mar 17 '12 18:03

Kenny Wyland


2 Answers

I've run into the same warning and opened an Technical Support Incident. The engineer verified that the default was changed from "assign" to "strong" for reasons of consistency within ARC.

He said both the warning and the documentation are wrong and will be fixed. Until that is done, I would avoid the implicit default altogether!

Explicitly adding "strong" (as BJ Homer suggested) is a safe way to silence the warning and be compatible. But don't assume properties to be unretained by default. Always put "weak" or "assign" there, too.

Edit: The clang documentation now officially documents this change. The warning has been fixed.

Edit 2: Xcode 4.4 apparently includes the fix.

like image 70
nschum Avatar answered Oct 17 '22 03:10

nschum


It looks wrong that it converted nonatomic, retain to nonatomic. I've always seen it convert to nonatomic, strong. If you can produce a simple project that converts in the way you saw it then I suggest filing a radar with it.

I assume by the way that you're using the latest Xcode.

like image 22
mattjgalloway Avatar answered Oct 17 '22 03:10

mattjgalloway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!