title: iphone EXC_BAD_ACCESS with NSMutableArray
body: <p>Ok so I have a UIViewTable and a UISearchBar with two scope buttons. The idea being that when I press a scope button the datasource for UIViewTable gets changed but I am getting 
  EXC_BAD_ACCESS error.</p>
  
  <p>I have the following code in my UIViewController  SearchViewController.m:</p>
  
  <pre><code>- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange: (NSInteger) selected scope
  {
      MyAppDelegate *delegate = (MyAppDelegate *) [[UIApplicationsharedApplication] delegate];
      if (self.listData != nil) {
          [self.listData release];
      }
      if (selectedScope == 0) {
          self.listData = [delegate.data getListOne];
      }
      else {
          self.listData = [delegate.data getListTwo];
      }
  }
  
  - (void) viewDidLoad {
      MyAppDelegate *delegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
      self.listData = [delegate.data getListOne];
  
      //some other unrelated code
  }
  </code></pre>
  
  <p>in my SearchViewController.h I have:</p>
  
  <pre><code>@property (nonatomic,retain) NSMutableArray *listData;
  </code></pre>
  
  <p>in my Data.m I have:</p>
  
  <pre><code>-(NSMutableArray *) getListOne {
       NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"test1",
                                                                      @"test2",
                                                                      nil];
       [list autorelease];
       return list;
  }
  
  -(NSMutableArray *) getListTwo {
       NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"test3",
                                                                      @"test4",
                                                                      nil];
       [list autorelease];
       return list;
  }
  </code></pre>
  
  <p>It crashes on:</p>
  
  <pre><code>self.listData = [delegate.data getListTwo];
  </code></pre>
  
  <p>I checked that its when I am setting the property that it crashes. My understanding is that when I create the new NSMutableArray in Data.m I assign it autorelease as I should. </p>
  
  <p>When the view loads I assign it to my listData and since I am accessing the property which has retain then the reference count is incremented (so its now 2 pending auto release).</p>
  
  <p>When I press the button to change the data source I check too see if listData exists (which it always will), release it so that the old NSMutableArray counter will be 0 (assuming autorelease has occured). </p>
  
  <p>Then I get a new NSMutableArray and set it to this property... is my understanding correct? I have spent far too long on this simple problem :(</p>
  
  <p>oh also I did create another NSMutableArray that was not connected to the tableView and still get the same problem, also if I don't release it in my if statement the problem does not exist but I will then have a memory leak?? I could always just keep the array and remove/add objects but I want to know why this is not working :)
  cheers</p>