views:

387

answers:

2

I get an EXEC_BAD_ACCESS when executing the following steps:

STEP 1: Click on "Scores" button from a Main Menu: This Removes the Menu (UIView), and loads the Scores (UIView) which subsequently initiates the process of populating a UITableView with values. No problems here.

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

STEP 2: Click on "menu" button from the Scores Screen: This Removes the Scores (UIView), and loads the Menu (UIView) again. No problems here.

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

STEP 3: Click on the "Scores" button again from the Main Menu: Again, this Removes the Menu (UIView), and loads the Scores (UIView) which subsequently initiates the process of populating a UITableView with values. Problematic!

The application crashes before displaying the Score Screen (UIView). Using the debugger I traced the problem to the single line of code: cell.text = [self->theScoresArray objectAtIndex:indexPath.row]; appearing in the below routine:

    - (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 this be a problem with the indexPath object not being properly released the first time? Any insight would be helpful. Thanks for your valuable time.

+1  A: 

Is "theScoresArray" released before you click on the "Scores" button again? Hovering on "theScoresArray" and see if it has values.

iPhoney
Yes, "theScoresArray" is released before the UIView is removed from the Superview. Could I be mistaken in removing views from the window and adding them each time the screen is switched? The UITableView is contained by a UIScrollView which is owned by the UIView. I am just using a UIViewController subclass to talk directly to said UITableView.
RexOnRoids
You need to add value for "theScoresArray" after you add subview for window. [window addSubview:viewController4.view]; viewController4.theScoresArray=...
iPhoney
+1  A: 

I think your problems lie with your use of UIWindow...

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

If you plan on displaying more than one view you should either:

  1. Use a UINavigationController or a UITabViewController.
  2. Add and remove subviews to the UIView in your single view app.

UIApplication needs to know who the on screen view is and certain delegate messages can get lost if you don't use these patterns (i.e. viewDidLoad, viewDidDisappear, etc..).

I ran into similar problems trying to manage my own views.

It sounds like in your instance you should be using a UINavigationController and pushing and popping your view controllers. It manages all of the initialization and cacheing and deallocating for you. That should lead to less issues.

It may seem like you can "roll you own", but fighting the SDK can be harder than learning to use it's conventions.

Corey Floyd
You were correct. I overhauled my View management structure and the problem vanished. Why that was a problem exactly is still a mystery though. I appreciate the help. Thanks.
RexOnRoids
no problem, it's a hard lesson. Apple does a lot "behind the scenes" with UIViewController which forces you to use their conventions.
Corey Floyd