I’ve wrote some code to batch delete/insert sections and cells from/into a table view. But I encountered a problem I can’t understand. My codes are as follow
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] initWithCapacity:1];
NSIndexSet *insertSections = [[NSIndexSet alloc] initWithIndex:kDeleteSection];
NSArray *insertDeleteIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:kDeleteSection], nil];
/// init deleteIndexPaths and insertIndexPaths
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView insertSections:insertSections withRowAnimation:UITableViewRowAnimationRight];
[self.tableView insertRowsAtIndexPaths:insertDeleteIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[insertSections release];
[deleteIndexPaths release];
//[insertSections release]; // If uncommented it, program will crash.
[insertDeleteIndexPaths release];
As the comment on the code, if I uncomment the statement [insertSections release];, the program will crash. Why? I can’t find the answer.
Because you are releasing it twice: