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-14-2009, 12:03 PM   #1 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 10
Default UIViewcontroller dealloc not being called

My app has a main UIViewController that loads a custom UIViewController for each screen in my app.

Its a linear experience, so the main controller loads the 1st screen. Then, when the player clicks a button, it loads the 2nd screen and releases the 1st one. For some reason, one of the UIViewControllers is being retained and so even though I'm releasing it, dealloc is not being called. I've seen several similar posts, but no clear solution. I have read all of the articles related to NIB files and memory management. I'm not sure I understand completely, but I can see no difference in how I am instantiating the UIViewController that isn't getting the dealloc call.

I would appreciate any suggestions.

Here is the header for my main controller:

Code:
#import <UIKit/UIKit.h>

@class JoinViewController;
@class LoginViewController;

@interface SwitchViewController : UIViewController {
	JoinViewController *joinViewController;
	LoginViewController *loginViewController;
}

@property (retain, nonatomic) JoinViewController *joinViewController;
@property (retain, nonatomic) LoginViewController *loginViewController;

- (void)initJoinView;
- (void)initLoginView;
- (void)launchLogin:(UIViewController *)sender;
- (void)launchGame:(UIViewController *)sender;

@end
Here is the implementation file for the main controller:

Code:
- (void)initJoinView {
	joinViewController = [[JoinViewController alloc] initWithNibName:@"JoinView" bundle:nil];
	self.joinViewController.rootWindowRef = self;
	[self.view insertSubview:joinViewController.view atIndex:0];
}

- (void)initLoginView {
	loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
	loginViewController.rootWindowRef = self;
    [self.view insertSubview:loginViewController.view atIndex:0];
	[self.loginViewController displayPref:self.prefObj];
}

- (void)launchLogin:(UIViewController *)sender {
	[self initLoginView];
	[[joinViewController view] removeFromSuperview]; 
	[joinViewController release]; 
}


- (void)launchGame:(UIViewController *)sender {
	[self initGameView];
	[[loginViewController view] removeFromSuperview];
	[loginViewController release];
}

The loginViewController gets the dealloc message. The joinViewController does not.

For fun, I reversed the order and loaded the loginViewController first and the
joinViewController second. Same results.
sheisdig is offline   Reply With Quote
Old 05-14-2009, 12:24 PM   #2 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 10
Default issue solved

I figured it out - I was setting UIView animation delegate which prevented the view from being released.
sheisdig is offline   Reply With Quote
Old 06-02-2009, 04:55 PM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 2
Default Clarification

I'm having a similar issue, using a pair of custom buttons to go from a UIView to UITableViews, could you clarify how you corrected the issue?

Thanks!
caleboller is offline   Reply With Quote
Old 06-02-2009, 05:04 PM   #4 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 2
Default

Update: Figured it out. Thanks
caleboller is offline   Reply With Quote
Old 06-02-2009, 07:47 PM   #5 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 10
Default Clarification on solution

The issue had to do with IBOutlets within UIViewControllers. I resolved my issue by adding this method:

Code:
-(void)setView:(UIView*)aView {
	if (!aView){
		//set outlets to nil here
		self.scrollView = nil;
		self.joinButton = nil;
		self.activityProgress = nil;
		self.numPlayersLabel = nil;
		self.numPlayersText = nil;
		self.timeView = nil;
		self.timeLabel = nil;
	}
	[super setView:aView];
}
And then in your dealloc method, release IBOutlets and set to nil.

Code:
- (void)dealloc {
	[scrollView release], scrollView = nil;
	[joinButton release], joinButton = nil;
	[activityProgress release], activityProgress = nil;
	[numPlayersLabel release], numPlayersLabel = nil;
	[numPlayersText release], numPlayersText = nil;
	[timeView release], timeView = nil;
	[timeLabel release], timeLabel = nil;
        [super dealloc];
}
I think I also may have had an IBOutlet instance that I didn't actually declare as an outlet in my header file.

Hope that helps.
sheisdig is offline   Reply With Quote
Old 09-27-2009, 11:15 AM   #6 (permalink)
mga
Registered Member
 
Join Date: Sep 2009
Posts: 1
Default

I have a similar situation to yours. I have managed to get the dealloc to fire but the nib is still around taking up memory. What am I missing?

I have a more detailed question (with code and all) posted here: iphone app with multiple views/subviews: memory is not being deallocated - Stack Overflow
mga is offline   Reply With Quote
Old 04-19-2010, 10:16 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 1
Default Viewcontroller dealloc not being called

I have a simple view controller that is switching to another view controller. When the switch occurs the dealloc method of the current view controller is not being called. I cannot see any references being left open. Note: Item is a core data object.

header file:

Code:
#import <UIKit/UIKit.h>

@interface AddCollectionViewController : UIViewController {
	IBOutlet UITextField *tfCollection;
	IBOutlet UITextField *tfYear;
	NSManagedObjectContext *managedObjectContext;
}

@property (nonatomic, retain) UITextField *tfCollection;
@property (nonatomic, retain) UITextField *tfYear;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

-(IBAction) btnClicked: (id) sender;
-(IBAction) cancelClicked: (id) sender;
-(void)insertNewObject;

@end
implementation:

Code:
#import "AddCollectionViewController.h"
#import "CollectionListViewController.h"
#import "Collection.h"

@implementation AddCollectionViewController

@synthesize tfCollection,tfYear,managedObjectContext;

-(IBAction) btnClicked:(id) sender {
	[self insertNewObject];

	CollectionListViewController *viewController = [[CollectionListViewController alloc] initWithNibName:@"CollectionListViewController" bundle:nil];
	viewController.managedObjectContext = [[UIApplication sharedApplication].delegate managedObjectContext]; 
	[self.navigationController pushViewController:viewController animated:YES];
	[viewController release];
}

-(IBAction) cancelClicked:(id) sender {
	CollectionListViewController *viewController = [[CollectionListViewController alloc] initWithNibName:@"CollectionListViewController" bundle:nil];
	viewController.managedObjectContext = [[UIApplication sharedApplication].delegate managedObjectContext];
	[self.navigationController pushViewController:viewController animated:YES];
	[viewController release];
}


#pragma mark -
#pragma mark Add a new object

- (void)insertNewObject {
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collection" inManagedObjectContext:managedObjectContext];
	Collection *coll = (Collection*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:managedObjectContext];
	
	coll.name = tfCollection.text ;
	coll.year = tfYear.text ;
	
	// Save the context.
    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
		NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
		abort();
    }
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	NSLog(@"AddCollectionViewController: viewDidUnload");
	self.tfCollection = nil ;
	self.tfYear = nil ;
}


- (void)dealloc {
	NSLog(@"In dealloc for AddCollectionViewController");
	[tfCollection release];
	[tfYear release];
	[managedObjectContext release];
    [super dealloc];
}


@end
cheyengu is offline   Reply With Quote
Reply

Bookmarks

Tags
dealloc, memory, nib, retain count, uiviewcontroller

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: 250
22 members and 228 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, ryantcb, Speed, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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