views:

249

answers:

2

Someone has posted a similar question to this with no resolution, but also no sample code. So, I thought I would post my problem in detail here.

I have a game with several modes of play, each of these having several options. After playing around with various designs, it seems cleanest to put them into a UITabBarController with three tabs, one for each class of games. I created a new UIVIewController which is loaded from the main menu screen (replacing the main screen) and initializes the UITabBarController as follows:

barController = [[UITabBarController alloc] init];

Games1 *vc1 = [[[Games1 alloc] initWithNibName:@"Games1" bundle:nil] autorelease];
Games2 *vc2 = [[[Games2 alloc] initWithNibName:@"Games2" bundle:nil] autorelease];
Games3 *vc3 = [[[Games3 alloc] initWithNibName:@"Games3" bundle:nil] autorelease];

NSArray* controllers = [NSArray arrayWithObjects:vc3, vc1, vc2, nil];
barController.viewControllers = controllers;
[self.view addSubview:barController.view];

When the user selects a game, I remove the UIViewController from the window and deallocate as follows:

- (void)dealloc {
    printf("Games released: barController: %d\n", [barController retainCount]);
    [barController.view removeFromSuperview];
    barController.viewControllers = 0;
    [barController release];
    barController = 0;
    [super dealloc];
}

The problem I have is that when I rotate the device, I get a crash. If I launch a game mode directly from the main screen and rotate, no crash. I've verified that everything is getting deallocated, and my retain count on the bar controller is 1. Any suggestions on how to eliminate this crash? Thanks!

[Edit] A bit more info:

The barController is defined as:

IBOutlet UITabBarController *barController;

with:

@property (nonatomic, retain) IBOutlet UITabBarController *barController;
A: 

Don't do this:

barController.viewControllers = 0;

In -dealloc you should only remove the UITabBarController's view from its superview and release it.

Costique
Thanks for the suggestion. I put that line in to try to fix things; removing it doesn't make a difference.
Nathan S.
+1  A: 

It ends up the problem was only peripherally related to the UITabBarController. I was adding and removing UIViewControllers directly to my app window, which has been shown to be causing problems elsewhere. Adding a master UIViewController / UIView and only adding and removing from that fixes everything, although an autorelease instead of a release may have worked as well. See the discussion here:

http://stackoverflow.com/questions/1585688/view-controller-being-sent-a-message-even-though-it-has-been-deallocated

The UITabBarController was just causing the problem to happen much more quickly and obviously.

Nathan S.