开发者

What should be the retain Count here + iPhone

I have following lines o开发者_JS百科f 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.


I stopped printing the retainCount when I don't know what a class is doing.

Maybe the navigationController is retaining your controller more times during a short moment.

But :
Alloc => +1
pushViewController => +1

:-)


I believe nothing is wrong. How many retain count increment does not matter. What matter is that when the view controller has been popped out, it must be released as many time as it has been retained.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜