MKMapView still sending messages to delegate after it's superview has been de-alloc'ed

前端 未结 2 1966
臣服心动
臣服心动 2021-02-19 07:52

EDIT: changed the title. I didn\'t know it at the time but this is a duplicate of Why am I crashing after MKMapView is freed if I'm no longer using it?


This q

相关标签:
2条回答
  • 2021-02-19 08:26

    MKMapView is not compiled with ARC and because of that the property for delegate is still declared as assign instead of weak.
    From the MKMapView documentation:

    @property(nonatomic, assign) id<MKMapViewDelegate> delegate
    

    And from the Transitioning to ARC Release Notes:

    You may implement a dealloc method if you need to manage resources other than releasing instance variables. You do not have to (indeed you cannot) release instance variables, but you may need to invoke [systemClassInstance setDelegate:nil] on system classes and other code that isn’t compiled using ARC.


    For delegates of system classes (NS*, UI*) you have to use the "old" rule of setting delegates to nil when you deallocate the delegate object.

    so add a dealloc method to your detailViewController

    - (void)dealloc {
        self.mapView.delegate = nil;
    }
    
    0 讨论(0)
  • 2021-02-19 08:35

    While it's true that the delegates for such classes should be explicitly set to nil, doing it in dealloc is already too late. You already lost your reference to the mapview during viewDidUnload. You should do the self.mapView.delegate = nil BEFORE viewDidUnload (so probably viewWillDisappear or viewDidDisappear)

    From my experience, only MKMapView and UIWebView behave this way.

    0 讨论(0)
提交回复
热议问题