I am using ARC but reading the MRR part of Objective-C, and it seems like if a property of ViewController is (for non-ARC):
@property (retain, nonatomic) Foo *foo;
then the viewDidLoad of ViewController will need to do a release right after alloc and init:
- (void)viewDidLoad
{
[super viewDidLoad];
self.foo = [[Foo alloc] init];
[self.foo release];
}
Otherwise, the retain will increment the reference count of the Foo object once when it is assigned to _foo (the instance variable), and alloc also increment the reference count once, so it is claiming ownership twice, and therefore, there needs to be a release right after the alloc and init?
I just feel it is a bit weird looking because an alloc is immediately followed by a release this way.
(If we do is a self.foo = [Foo fooByString: @"hello"], then one ownership is claimed by the autorelease pool, and one claimed by ViewController, and at the end of the event loop, the autorelease pool drains, and unclaim one ownership, and therefore the Foo object is correctly owned once only. (but if Foo doesn’t have such methods and only have alloc and init, then the immediate release is needed.))
You’re essentially correct, though there are a few ways that it was typically done to make it look less awkward:
Or more succinctly: