dismissViewControllerAnimated仅在iOS 7.1中导致EXC_BAD_ACCESS

时间:2014-03-14 00:01:24

标签: ios objective-c ios7.1

ViewControllerA使用模态segue打开ViewControllerB

ViewControllerA

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // ModalSegue is defined in the storyboard to point to ViewControllerB
    [self performSegueWithIdentifier:@"ModalSegue" sender:self];
}

ViewControllerB

- (IBAction)cancelButtonTapped:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil]; // Causes crash
}

在iOS 7.1中,这会导致EXC_BAD_ACCESS崩溃。如果启用了Zombie Objects,则抛出异常:

*** -[ViewControllerB respondsToSelector:]: message sent to deallocated instance 0x12ed7e170

在iOS 7.0中,这可以按预期工作。

有什么想法吗?

编辑:根据LeoNatan的请求,这里是deallocViewControllerB方法的堆栈跟踪:

Stack Trace

1 个答案:

答案 0 :(得分:6)

正如聊天中所讨论的,问题是选择器视图的寿命比视图控制器长,导致它尝试向其代理发送消息。

解决方案是在nil方法中将选择器视图的委托和数据源设置为dealloc

对于iOS 7及更高版本,将代理和数据源设置为nil被认为是一种好习惯,因为视图具有比视图控制器更长的生命周期,并且在这些视图被释放后尝试访问它们的代理。 / p>