سؤال

I've just upgraded my application to ARC, but I am having issues with how it handles classes with delegates. Because I am targeting iOS 4.0 and above, I am using __unsafe_unretained and @property (unsafe_unretained) to store the delegate pointers.

However, what I am now finding is that the services (that include delegates) I create in my view controllers are now hanging around after I have removed that view controller, which result in trying to communicate back with the view controller (delegate) after it has been deallocated... And therefore getting an error with Zombies enabled.

How can I update my code in either the view controller or the service, so that when the view controller is removed (popped off the nav controller stack) the service with the delegate pointer also gets removed from memory?

هل كانت مفيدة؟

المحلول

It turns out that it's not me that's doing anything wrong...

The offending NSZombie turned out to be due to the MKMapViewDelegate, which keeps sending 'mapView:didUpdateUserLocation:' after the View Controller it belongs to gets deallocated. I know.

I resolved it by intercepting the Back button in my View Controller, and set the delegate to nil before popping it:

- (void)backButtonTapped
{
    self.mapView.delegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

I don't know why MapKit holds onto a delegate after it has been dealloc'd. If anyone knows I'd be grateful to find out more.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top