Let's say I have a strong property like so:
@interface Foo
@property (strong, nonatomic) NSArray *myArray;
@end
And, in my initializer, I set myArray like so:
myArray = [NSArray array];
Is this safe? Will ARC take care of properly retaining myArray for me?
The reason I ask is that I have a project where myArray isn't properly retained in this scenario, and I get a bad memory access down the road.
But, if I use
myArray = [[NSArray alloc] init];
then all is well.
Yes, ARC will automatically retain it for you.
The way to think of ARC is this: If you have a strong pointer to an object, then it is guaranteed to stay alive. When all pointers (well, all strong pointers) to an object go away, the object will die.
From the description of your problem, it sounds like ARC isn't properly enabled in the file where you're executing that code. Regardless, I'd recommend running your app with Instruments, using the "Zombies" template. That will let you see the full retain/release history of that object, and you should be able to figure out where things are going wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With