EXEC_BAD_ACCESS when reloading UIViewController containing UITableView

I am getting EXEC_BAD_ACCESS when doing the following steps:

STEP 1: Click the Count button in the main menu: This removes the menu (UIView) and loads the results (UIView), which subsequently initiates the process of populating the UITableView with values. No problem here.

            self.viewController4 = [[ScoresViewController alloc]  initWithNibName:@"ScoresViewController" bundle:nil];
        [window addSubview:viewController4.view];
        [viewController.view removeFromSuperview];
        NSLog(@"LOADING SCORES SCREEN");

      

STEP 2: Click the "menu" button on the "Rating" screen: This removes the ratings (UIView) and reloads the menu (UIView). There is no problem here.

self.viewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
        [window addSubview:viewController.view];
        [viewController4.view removeFromSuperview];
        NSLog(@"Loading MAIN MENU");

      

STEP 3: Click the Count button again from the main menu: Again, this removes the menu (UIView) and loads the results (UIView), which subsequently initiates the process of populating the UITableView with values. Distressed!

The app crashes before displaying the evaluation screen (UIView). Using the debugger, I traced the problem with one line of code: cell.text = [self-> theScoresArray objectAtIndex: indexPath.row]; that appears in the following procedure:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [self->theScoresArray objectAtIndex:indexPath.row];
return cell;
}

      

Could there be a problem with the indexPath object not being released correctly the first time? Any insight would be helpful. Thanks for your precious time.

+1


a source to share


2 answers


I think your problems are related to using UIWindow ...

You shouldn't remove / add views directly to the UIWindow instance in your application.

If you plan to display multiple views, you must:

  • Use UINavigationController or UITabViewController.
  • Add and remove subviews to UIView in your single view.


UIApplication needs to know who is on screen and some delegate messages might get lost if you don't use those templates (i.e. viewDidLoad, viewDidDisappear, etc.).

I faced similar problems while trying to manage my own views.

It looks like in your example you should be using a UINavigationController and pop and display your view controllers. It manages all initialization and caching and freeing from you. This should lead to fewer problems.

It may seem like you can "quit smoking", but fighting the SDK can be more difficult than learning how to use it.

+1


a source


Is "TheScoresArray" open before you click the Score button again? Hover over "theScoresArray" and see if it has values.



+1


a source







All Articles