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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-28-2009, 05:20 AM   #1 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
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 05:23 AM.
CDCMacDaddy is offline   Reply With Quote
Old 05-28-2009, 09:04 AM   #2 (permalink)
Coder for Life
 
Karl Kraft's Avatar
 
Join Date: Sep 2008
Location: Austin, TX
Posts: 76
Send a message via AIM to Karl Kraft
Default

Quote:
Originally Posted by CDCMacDaddy View Post
Code:
  [window addSubview:[newViewController view]];
  NSLog(.......[newViewController retainCount]]);
Adding a view is not going to change the retain count of the views controller. If you are getting an increase in the retain count of the second view controller it is not from the addSubview: method. Something else is retaining the second view controller.
Karl Kraft is offline   Reply With Quote
Old 05-28-2009, 02:04 PM   #3 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default Solved - NSTimer is doing a retain.

Hi - Thanks for your reply. I did more digging, and it appears that NSTimer is retaining the viewController (the vc holds the target method of that timer).
CDCMacDaddy is offline   Reply With Quote
Old 06-01-2009, 05:55 AM   #4 (permalink)
New Member
 
Join Date: May 2009
Posts: 1
Default

Quote:
Originally Posted by CDCMacDaddy View Post
Hi - Thanks for your reply. I did more digging, and it appears that NSTimer is retaining the viewController (the vc holds the target method of that timer).
How did you get rid of this issue?
arj23 is offline   Reply With Quote
Old 06-02-2009, 03:04 PM   #5 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default

Quote:
Originally Posted by arj23 View Post
How did you get rid of this issue?
I've not gotten back to this in a few days... Still working with it.

I have a VC owning the thread and also the target. I've been trying to invalidate and release the thread in the dealloc of the vc, but that's not been happy.

My next attempt will be a method in the vc that invalidates and releases the timer separate from its own dealloc, then have the appDelegate initiate this cleanup before it wants to release the vc.
CDCMacDaddy is offline   Reply With Quote
Reply

Bookmarks

Tags
addsubview, release, retain count, viewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 271
17 members and 254 guests
ADY, Alsahir, Dani77, Desert Diva, Duncan C, F_Bryant, Grinarn, HemiMG, Herbie, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,761
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:44 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0