Why is this over-releasing? UINavigationController

2019-03-25 16:52发布

问题:

I'm pushing a view controller onto my navigation controller's stack from within my TableViewController's didSelectRowAtIndexPath method as so:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
MyObject *myO = (MyObject *)[appDelegate.myOs objectAtIndex:indexPath.row];
myViewController.amount = myO.amount;
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];

If I uncomment that last line then the app crashes upon return with an error:

-[CALayer release]: message sent to deallocated instance 0xd4f860

Digging in a bit further I find the crash can further be narrowed down to the call to [super dealoc] within MyViewController's dealoc method. By uncommenting "[super dealoc]" we don't crash.

I'm having trouble narrowing this down further. "super" would be the UIViewController class and I can't further investigate that dealoc method...can I? Maybe there's a way to see what exactly 0xd4f860 was pointing to but I'm not aware how? Any ideas?

回答1:

You're looking in the wrong place -- the problem is that myViewController is getting released (or autoreleased) too many times.

The code you have posted looks correct, so I'd look at MyViewController's code to see if it ever releases itself, or somehow causes itself to be released by other means.

You could also override the release method, set a break point and see if you can narrow it down that way... e.g.

- (void)release {
    [super release]; //Set breakpoint here
}