开发者

How do I prevent quickly tapping the Edit button for a UITableView multiple times from crashing the application?

I have a subclassed UITableViewController that performs som开发者_开发技巧e animations when editing mode is entered, namely removing the UIBarButtonSystemItemAdd from a navigation bar, and animating the first row of the table out of existence, and then back in when edit mode is exited:

- (void) setEditing: (BOOL) editing animated: (BOOL) animated
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];

    if (editing)
    {
        [self removeAddButton];
        [datasource removeObjectAtIndex: 0];
        [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationTop];
    }
    else
    {
        [self restoreAddButton];
        [datasource insertObject: @"First Row" atIndex: 0];
        [tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationTop];
    }

    [indexPath release];
    [super setEditing: editing animated: animated];
}

Problem is, when the Edit button is tapped too quickly multiple times, and the animations haven’t finished yet, the application crashes with:

AppName(1824,0xa0ae5500) malloc: *** error for object 0x5f503a0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Obviously, something is going on where something being animated in or out isn’t having enough time to finish, so, how do I get around this? Am I doing something unnecessary/wrong?


Actually, this is a simple memory management error.

Remove this line:

[indexPath release];

The reason? This line:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];

This returns a NSIndexPath with an effective retain count of 0. You don't need to release it.

For any kind of memory release error, you should run again with NSZombieEnabled turned on. It will give you a more complete error message.

You should also get in the habit of using Build & Analyze; it probably would have flagged this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜