I’m trying to figure out why pushing a viewController in my tableView using didSelectRowAtIndexPath would cause a crash in iOS 4.3, but in iOS 5.0+, it works fine.
It crashes right when I call:
self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease];
anytime after the first time the customViewController has been pushed.
Here’s my relevant code:
@property (nonatomic, retain) CustomViewController *customViewController;
-(void) dealloc // Dealloc of tableView.
{
[customViewController release];
customViewController = nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.customViewController = [[[CustomViewController alloc] initWithNibName:@"CustomViewController"bundle:nil] autorelease]; // Release old, allocate new, set it.
[[self navigationController] pushViewController:customViewController animated:YES];
[customViewController release]; // Balance out pushViewController's retain.
}
Thanks.
The last
releaseis an extra one that is not needed.You already have done an
autoreleaseon it to have it’sretain countdown by one.We will analyse this line
you create a
CustomViewControllerretain count == 1.You say
autoreleaseon it so retain count will be 0 later (probably the end of the run loop), but for now it’s still 1 that is why you still have access to it, but treat it as 0.After that you say
self.customViewController, that property is retain, so retain count == 1.And you are taking care of that 1 in your dealloc.
As of your comment :
You don’t
Balancethose, you balance only the oneYOU own. If the system make retain on your objects, it will release them when the system don’t need them anymore.