Why is this over-releasing? UINavigationController / UITableview

别来无恙 提交于 2020-01-01 04:53:06

问题


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
}


来源:https://stackoverflow.com/questions/1167400/why-is-this-over-releasing-uinavigationcontroller-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!