I wrote the following piece of code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
GameViewController *gameViewController = [[GameViewController alloc]initWithLevelNumber:([levelGroup intValue]*100+indexPath.row) Bonus:NO];
NSLog(@"Retain Counter =%d",gameViewController.retainCount);
[navController pushViewController:gameViewController animated:YES];
[gameViewController release];
NSLog(@"Retain Counter=%d",gameViewController.retainCount);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
The results of the two logs are, in sequence 1 and 6!
How this is possible? I only call the alloc method one time and release after push the controller on the stack.. alloc-> +1, push-> +1, release-> -1 = 1 or not?
I’d like the view controller is dealloc’d when i pop it off the stack..
Autorelease your GameController creation, like this:
Then delete
[gameViewController release]; Then your code looks kosher, andgameViewControllerwill be autoreleased after being popped from the nav stack. Don’t worry aboutretainCount– when you push a view controller,UIKittakes over and willretain/releasethe thing as needed. You just have to worry about your code. Actually, the way you have it written should be fine, I just think my suggestions here make the code cleaner.Unless you see in Instruments that you have a memory leak of your
gameViewControllerobject, I think you needn’t worry.