问题描述
我有 MKMapView
。有时在我的视图控制器被解雇后,我将获得 EXC_BAD_ACCESS
。
I have a MKMapView
. Sometimes after my view controller is dismissed, I'll get a EXC_BAD_ACCESS
.
我打开 NSSZombies
它看起来像 MKMapView
的委托 - 我的视图控制器! - 正在调用,尽管已释放 MKMapView
和 UIViewController
子类。我已经检查了,我的记忆管理是正确的。
I turned on NSSZombies
and it looks like the MKMapView
's delegate — my view controller! — is being called, despite both the MKMapView
and UIViewController
subclass being freed. I've checked, and my memory management is correct.
发生了什么事?
推荐答案
这是因为 MKMapView
的工作原理。有一个挂起的操作,所以MapKit保留了 MKMapView
,它实际上还没有被释放。这不是一个问题。问题是它仍然向你的代表发送消息。
This is because of the way MKMapView
works. There's an operation pending, so MapKit is retaining the MKMapView
and it hasn't actually been deallocated yet. That isn't itself a problem. The problem is that it's still sending messages to your delegate.
解决方法很简单:作为视图控制器清理的一部分,将地图视图的委托设置为 nil
,这将阻止 MKMapView
向其发送消息。
The workaround is simple: As part of your view controller's cleanup set the map view's delegate to nil
, which will prevent MKMapView
from sending messages to it.
这是记录在中:
编辑:给Oscar一个upvote ,在下面,谁在这里提供了文档引用。
Give Oscar an upvote as well, just below, who provided the documentation quote here.
鉴于ARC,我建议这意味着你应该将地图视图的委托设置为 nil
在视图控制器的 dealloc
。
Given ARC, I suggest this means you should set your map view's delegate to nil
in your view controller's dealloc
.
这篇关于如果我不再使用MKMapView,为什么我会崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!