Search code examples
iphoneobjective-cretain

Objective-C retain clarification


I'm looking at this code:

NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];

Later on...

- (void)dealloc {
    [viewControllers release];
    ...
}

I see that self.viewControllers and controllers now point to the same allocated memory (of type NSMutableArray *), but when I call [controllers release] isn't self.viewControllers released as well, or is setting self.viewControllers = controllers automatically retains that memory?


Solution

  • The dot-notation (self.foo = bar;) equals calling [self setFoo:bar];. If your property is declared to retain its value, then your viewcontrollers will retain the array in this case, and release it once you set a new value.