0

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???

Rost K.
  • 262
  • 2
  • 14
  • structure of all viewController UITabBarController - rootViewController --UINavigationController ----UIViewController ------`viewController` - `pushViewController:animated:` I'm trying to release rootViewController - all views are released only `viewController` has problems. Before releasing rootViewController I'm calling - (void)unload – Rost K. Aug 02 '12 at 01:33

2 Answers2

1

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.

mamackenzie
  • 1,156
  • 7
  • 13
  • It's not worked. Only if I'm using [_viewController release]; and self.viewController = nil; dealloc is calling, BUT twice. Maybe this helps: _viewController has MPMoviePlayerController which is playing... – Rost K. Aug 02 '12 at 01:11
  • @RostK. You have an extra retain then, because that code will release it twice. Use instruments to track to retains and releases of this variable. – borrrden Aug 02 '12 at 02:57
0

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.

newacct
  • 119,665
  • 29
  • 163
  • 224