Dylan Hand
Dylan Hand

Reputation: 1400

dismissViewControllerAnimated causes EXC_BAD_ACCESS in iOS 7.1 only

ViewControllerA opens ViewControllerB using a modal segue.

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
}

In iOS 7.1, this causes a EXC_BAD_ACCESS crash. If Zombie Objects is turned on it throws the exception:

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

In iOS 7.0, this works as expected.

Any ideas?

EDIT: As per LeoNatan's request, here's the stack trace of the dealloc method in ViewControllerB:

Stack Trace

Upvotes: 0

Views: 3308

Answers (1)

Léo Natan
Léo Natan

Reputation: 57060

As discussed in chat, the problem is a picker view having a longer lifespan than its view controller, causing it to attempt sending a message to its delegate.

The solution is to set the delegate and data source of the picker view to nil in the dealloc method.

With iOS 7 and above, it is considered good practice to set delegates and data sources to nil, because views have a longer lifespan than their view controllers and try to access their delegates after these have been released.

Upvotes: 6

Related Questions