I am stuck on a EXC_BAD_ACCESS” crash within my dealloc of a table view controller. The crash occurs when releasing an NSMutableArray that was given a retain property. I have a second NSMutableArray that was also given a retain property, but it’s release does not cause a crash. Please take a look at the following code to see if I am overlooking something about memory management. Thanks.
In my header file, I have the following:
@interface selectSourcesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *selectedNames;
NSMutableArray *selectedAvailability;
}
@property (retain, nonatomic) NSMutableArray *selectedNames;
@property (retain, nonatomic) NSMutableArray *selectedAvailability;
In my implementation, I have the following:
@implementation selectSourcesTableViewController
@synthesize selectedNames;
@synthesize selectedAvailability;
- (void)viewDidLoad {
NSArray *names = [selectedSourceFileContent objectForKey:@"selectedNames"];
selectedNames = [[NSMutableArray alloc] initWithObjects: nil];
NSArray *availability = [selectedSourceFileContent objectForKey:@"selectedAvailability"];
selectedAvailability = [[NSMutableArray alloc] initWithObjects: nil];
for (int i=0; i < [names count]; i++) {
NSString *aName = [names objectAtIndex:i];
[selectedNames addObject: aName];
NSString *anAvailability = [availability objectAtIndex:i];
[selectedAvailability addObject: anAvailability];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];
...
for (int i=0; i < [selectedNames count]; i++) {
if ([contentForThisRow isEqualToString:[selectedNames objectAtIndex:i]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
- (void)dealloc {
[super dealloc];
[selectedNames release];
[selectedAvailability release];
}
The code shown above shows the only uses of these two arrays.
So, nothing bad happens when selectedNames is released, but I get a EXC_BAD_ACCESS crash when selectedAvailability is released.
One last observation. There is no crash the very first time I run this code after launching xcode. Thereafter, it crashes every time I rerun the app.
Any thoughts?
needs to be called last, not first, in your own dealloc method.