I have following lines of code in a program
VisitWebsiteVC *visitWebSite
= [[VisitWebsiteVC alloc] initWithNibName:@"VisitWebsiteVC" bundle:nil];
NSLog(@"Retain Count :%i",[visitWebSite retainCount]);
[self.navigationController pushViewController:visitWebSite animated:YES];
NSLog(@"Retain Count :%i",[visitWebSite retainCount]);
[visitWebSite release];
In the console I see the print statement as
Retain Count :1
Retain Count :5
I am not getting why the line after I am pushing my viewController is returning retainCount of my viewController as 5, when it must be 2.
You don’t want to rely on the retain count for anything. There’s all sorts of stuff going on behind the scenes when you push a view controller (the view is instantiated, which may mean loading a XIB, there are a bunch of autorelease calls that haven’t fired yet). It’s a pretty dangerous way to check memory usage.
As to why it’s 5 and not 2, as I said earlier, it’s most likely related to unresolved autorelease pools. If you check the retainCount in the viewDidAppear, or, better yet, after all initialization calls have resolved, it might be closer to 2.