I have a strange problem with the following code:
MembersDetailViewController *m = [[MembersDetailViewController alloc]
initWithNibName:@"MembersDetailViewController" bundle:nil];
[m updateDataSource:[self.data objectAtIndex:[indexPath row]]];
[self.navigationController pushViewController:m animated:YES];
[m release];
This code is called in tableView didSelectRowAtIndexPath in my MembersListTableViewController controller. The MembersDetailViewController can open another view to display a map:
MapViewController *mapViewController = [[MapViewController alloc]
initWithNibName:@"MapViewController" bundle:nil];
mapViewController.delegate = self;
mapViewController.latitude = @"54.5944504";//self.latitude;
mapViewController.longitude = @"-5.6592376";//self.longitude;
[mapViewController setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:mapViewController animated:YES];
[mapViewController release];
The problem is that when the MapViewController is dismissed and the back button pressed on the MemberDetailViewController I get the following error message:
-[MembersDetailViewController retain]: message sent to deallocated instance 0x51663e0
If I comment out [m release] then everything works ok. If the MapViewController isn’t called everything is also ok. I don’t understand why this is happening? I was always under the impression that if I alloc init then I need to release?
Any help would be greatly appreciated.
You’re right that you do need to release
m, but in doing that you give up ownership of that view controller. It’s okay for a while, because you’ve pushed it onto the navigation stack, so the navigation controller retains it. However, when you hit the “Back” button, the nav controller pops it off the stack and releases it, and at that point it’s deallocated because nobody else owns it. If you refer to it again after that point, you’ll get an error like the one you described.So… if you need to refer to the view controller that
mpoints to in the method above, you’ll probably want to create a “retain” property for it in your table view controller and set it tombefore you releasem. Make sure to set that property to nil once you no longer need the detail view controller — that’ll cause it to finally be released and deallocated.