Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 05-28-2009, 04:20 AM   #1 (permalink)
CDCMacDaddy
New Member
 
Join Date: May 2009
Posts: 3
CDCMacDaddy is on a distinguished road
Question Inconsistent retainCount - addSubView?

Hi all,

I can't figure out why I'm getting inconsistent results from nearly identical code. I have two viewcontrollers that I'm loading as needed. As I understand it, addSubview is supposed to increase the retain count, but it is doing that in one method and not another. It's a problem because I'm calling release and expecting the viewController to tear down, but in one case it is and in another there's still a retainCount left keeping the VC alive...

I should add that in view2, I've got an NSTimer running, which starts during viewDidLoad. Would that be the culprit? If so, why would it retain its parent object?

What am I missing here?


Code:
-(void)gotoView1 {
	
	newViewController = [[ViewController1 alloc] initWithNibName:@"View1" bundle:nil];
	NSLog([NSString stringWithFormat:@"%@: View1 count is %d", [newViewController description] ,[newViewController retainCount]]);
	[[newViewController view] setAlpha:0];
	[[newViewController view] setBounds:CGRectMake(0,0, 480, 320)];
	[window addSubview:[newViewController view]];
	NSLog([NSString stringWithFormat:@"%@: View1 count is %d", [newViewController description] ,[newViewController retainCount]]);
	
	[UIView beginAnimations:@"windowFlip" context:NULL];
	[UIView setAnimationDuration:0.5];
	[[newViewController view] setAlpha:1];
	[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
	[UIView setAnimationDelegate:self];
	[UIView commitAnimations];

	//ASSIGN DATA STUFF HERE
}



-(void)gotoView2{
	
	newViewController = [[ViewController2 alloc] initWithNibName:@"View2" bundle:nil];
	NSLog([NSString stringWithFormat:@"%@: View2 count is %d", [newViewController description] ,[newViewController retainCount]]);
	[[newViewController view] setAlpha:0];
	[[newViewController view] setBounds:CGRectMake(0,0, 480, 320)];
	[window addSubview:[newViewController view]];
	NSLog([NSString stringWithFormat:@"%@: View2 count is %d", [newViewController description] ,[newViewController retainCount]]);
	
	
	[UIView beginAnimations:@"window2Flip" context:NULL];
	[UIView setAnimationDuration:0.5];
	[[newViewController view] setAlpha:1];
	[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
	[UIView setAnimationDelegate:self];
	[UIView commitAnimations];	
	
	//ASSIGN DATA STUFF HERE
	
}
The NSLog output looks like this:

<ViewController1: 0x116f40>: View1 count is 1
<ViewController1: 0x116f40>: View1 count is 1
<ViewController2: 0x15fbf0>: View2 count is 1
<ViewController2: 0x15fbf0>: View2 count is 2

Last edited by CDCMacDaddy; 05-28-2009 at 04:23 AM.
CDCMacDaddy is offline   Reply With Quote
 

» Advertisements
» Online Users: 463
19 members and 444 guests
apatsufas, baja_yu, buggen, chenpo, ChrisYates, Feldspar, HowEver, ilmman, IphoneSdk, jbro, jeroenkeij, jNoxx, RobTaku, rraagg, samdanielblr, swelsh, taylor202, Trickphotostudios, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,636
Threads: 94,098
Posts: 402,804
Top Poster: BrianSlick (7,990)
Welcome to our newest member, samdanielblr
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.