Reputation: 262
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
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
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