Rost K.
Rost K.

Reputation: 262

UIViewController not calling dealloc or calling it twice

I have some problem with my singleton and UIViewController there;

Singleton.h

@property (nonatomic, retain) UIViewController *viewController;

Singleton.m

...

@synthesize viewController = _viewController;

- (void)load {
   self.viewController = [[[UIViewController alloc] initWithNibName:@"NibName" bundle: nil] autorelease];
}

- (void)unload {
   [_viewController release];
}

This viewController using by different part of the application via pushViewController:animated:. But sometimes I need to release viewController by calling method - (void)unload of Singleton class! If pushViewController:animated: never call for viewController everything is well and dealloc is calling, but if pushViewController(and viewController perform viewDidLoad), dealloc isn't work. If I do something like self.viewController = nil; dealloc calling twice... What I'm doing wrong???

Upvotes: 0

Views: 933

Answers (2)

mamackenzie
mamackenzie

Reputation: 1166

Your unload function should only consist of:

- (void)unload {
   self.viewController = nil;
}

When you set a retained property to nil, it releases the instance variable AND nils it. You are simply leaving a dangling pointer on your property here.

Upvotes: 1

newacct
newacct

Reputation: 122519

You need to set it to nil after releasing it:

[_viewController release];
_viewController = nil;

Otherwise the next person who comes along will try to do stuff with an invalid pointer.

Upvotes: 0

Related Questions