tek3
tek3

Reputation: 2115

What should be the retain Count here + iPhone

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.

Upvotes: 2

Views: 404

Answers (3)

tia
tia

Reputation: 9708

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.

Upvotes: 1

Ben Gottlieb
Ben Gottlieb

Reputation: 85542

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.

Upvotes: 4

Vinzius
Vinzius

Reputation: 2901

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

:-)

Upvotes: 1

Related Questions