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.
I have the following code in my UIViewController SearchViewController.m:
- (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
}
in my SearchViewController.h I have:
@property (nonatomic,retain) NSMutableArray *listData;
in my Data.m I have:
-(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;
}
It crashes on:
self.listData = [delegate.data getListTwo];
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.
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).
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).
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 :(
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