I have a UITableView and I implemented the delete from table swipe method.
For some reason, the assignment of arrays is causing the app to crash.
I would like to know why.
two properties:
@property (nonatomic,retain) NSMutableArray *mruItems;
@property (nonatomic,retain) NSArray *mruSearchItems;
.
.
.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle != UITableViewCellEditingStyleDelete)
return;
NSString *searchString = [self.mruSearchItems objectAtIndex:indexPath.row];
[self.mruItems removeObject:searchString];
[self.mruSearchItems release];
// This line crashes:
self.mruSearchItems = [[NSArray alloc] initWithArray:self.mruItems];
[self.searchTableView reloadData];
}
It is as if after mruItems’s objects has been removed, it can’t help initialize mruSearchItems…
Thanks!
EDIT:
EXC_BAD_ACCESS
@synthesize mruItems,mruSearchItems; <–Debugger points here
It is double releasing causes crash.
This makes refcount -1
This makes refcount -1
Since mruSearchItems has “retain” in property attributes, your assign to it will cause another refcount -1.
So either remove the release line or set it to nil after release it and before assign to it.
Edit:
This line
causes memory leak, fix it like this:
or:
Edit Again
What does “retain” in property actually do?
Take mruSearchItems as example, when you assign it: