I am confuzed by the memory management of instance members. I have a class with a ivar:
DetailedResultsTableViewController *detailedResultsTableViewControlle
Using the property and synthesize gives you a new method. In this instance, you would have a new set and get method for the detailedResultsTableViewController
. This is generated for you when you compile (ie there is no code that you have to add)
This set method will be
- (void)setDetailedResultsTableViewController:(DetailedResultsTableViewController *)c
{
if (detailedResultsTableViewController != nil)
{
[detailedResultsTableViewController release];
detailedResultsTableViewController = nil;
}
detailedResultsTableViewController = [c retain];
}
So, when you call
self.detailedResultsMapViewController = [[DetailedResultsMapViewController alloc] init...];
What you are actually calling is
[self setDetailedResultsMapViewController:[[DetailedResultsMapViewControler...]]];
And so you are actually doing two retains. One where you are calling alloc...init. and then the other because you are implicitly calling the setDetailedResultsMapViewController which will then do a retain as well.
If you are using properties, you would use
DetailedResultsTableViewController *d = [[DetailedResultsMapViewController alloc] init...]
self.detailedResultsMapViewController = d;
[d release];
The benefit of this is that you don't have to remember to release the old object before assigning the new as the synthesized method does that for you. You can also just do
self.detailedResultsMapViewController = nil;
in your dealloc method and you won't have to worry if you have already released it elsewhere.
This is useful to know because you can override the set method by manually entering the code which allows you to do things when the objects are set.