Cocoa touch 如何防止多次快速点击UITableView的编辑按钮导致应用程序崩溃?
我有一个子类UITableViewController,它在进入编辑模式时执行一些动画,即从导航栏中删除UIBarButtonSystemItemAdd,并设置表格第一行不存在的动画,然后在退出编辑模式时返回:Cocoa touch 如何防止多次快速点击UITableView的编辑按钮导致应用程序崩溃?,cocoa-touch,ios,uitableview,Cocoa Touch,Ios,Uitableview,我有一个子类UITableViewController,它在进入编辑模式时执行一些动画,即从导航栏中删除UIBarButtonSystemItemAdd,并设置表格第一行不存在的动画,然后在退出编辑模式时返回: - (void) setEditing: (BOOL) editing animated: (BOOL) animated { NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
- (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];
}
问题是,当多次快速点击“编辑”按钮,并且动画尚未完成时,应用程序会因以下原因崩溃:
AppName(1824,0xa0ae5500) malloc: *** error for object 0x5f503a0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
显然,有些事情正在发生,一些正在被动画化的东西没有足够的时间来完成,所以,我该如何解决这个问题呢?我是否做了一些不必要/错误的事情?实际上,这是一个简单的内存管理错误 删除此行:
[indexPath release];
原因是什么?这一行:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
这将返回有效保留计数为0的NSIndexPath。你不需要释放它
对于任何类型的内存释放错误,应在启用NSZombieEnabled的情况下再次运行。它将向您提供更完整的错误消息
您还应该养成使用构建和分析的习惯;它可能会标记这个。我能给你买杯啤酒吗?非常感谢不客气。至于啤酒,我将告诉你我对每个人说的话:如果我们在一个开发者会议上见面,就会有两种方式购买啤酒,直到我们都记不起自己的名字注:更新答案,提及构建和分析。