使用 KVO 时出现异常
我正在实现基于 MKMapView 的应用程序。当我们点击图钉时,我正在使用观察者。观察者的代码如下,
[annView addObserver:self
forKeyPath:@"selected"
options:NSKeyValueObservingOptionNew
context:@"ANSELECTED"];
它按预期工作,但有时会出现异常“EXC_BAD_ACCESS”。我的日志如下,它显示内存泄漏。我需要释放服务器吗?如果我?那我应该在哪里发布这个呢?
An instance 0x1b21f0 of class MKAnnotationView is being deallocated
while key value observers are still registered with it. Observation
info is being leaked, and may even become mistakenly attached to
some other object. Set a breakpoint on NSKVODeallocateBreak to stop
here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x11e5f0> (
<NSKeyValueObservance 0x1b1da0: Observer: 0x120f70, Key path: selected, Options: <New: YES, Old: NO, Prior: NO> Context: 0x2b588, Property: 0x1acaa0>
I am implementing MKMapView based application. In that I am using an observer when we tap on a pin. the observer is code is follows,
[annView addObserver:self
forKeyPath:@"selected"
options:NSKeyValueObservingOptionNew
context:@"ANSELECTED"];
It is working as expected, but some time it is getting exception 'EXC_BAD_ACCESS'. My log is as follows and it is showing me a leaking memory. Do I need to release the server?. If I ? then where should I release this?
An instance 0x1b21f0 of class MKAnnotationView is being deallocated
while key value observers are still registered with it. Observation
info is being leaked, and may even become mistakenly attached to
some other object. Set a breakpoint on NSKVODeallocateBreak to stop
here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x11e5f0> (
<NSKeyValueObservance 0x1b1da0: Observer: 0x120f70, Key path: selected, Options: <New: YES, Old: NO, Prior: NO> Context: 0x2b588, Property: 0x1acaa0>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与泄漏相反。它正在被释放;泄漏是指对象永远不会被释放。
问题在于它正在被释放,而其他东西仍在观察它。任何仍在观察该对象的对象也可能稍后向其发送其他消息;当它发生时,这些消息将发送到一个死对象(导致您看到的崩溃,发生在该消息之后)或发送到另一个对象。
如果观察 MKAnnotationView 的对象拥有它并释放它,则在释放它之前需要将自己作为观察者删除。如果它不拥有它,它可能应该拥有它。
That's the opposite of a leak. It is being deallocated; a leak is when an object will never be deallocated.
The problem is that it's being deallocated while something else is still observing it. Anything that's still observing this object may also send it other messages later; when it does, those messages will go to a dead object (causing the crash you saw, which happened after that message) or to a different object.
If the object that is observing the MKAnnotationView is owning it and releasing it, it needs to remove itself as an observer before releasing it. If it does not own it, it probably should.
在释放注释视图之前,您必须停止观察它:
You have to stop observing the annotation view before you release it:
我知道它已经很老了,但我发现这段代码在 stackoverflow 和其他存储库中被大量使用,这里是问题的解决方案。
您应该在视图控制器类中创建一个 NSMutableArray ivar,以便存储对注释视图的引用:
}
在 viewDidLoad 和
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id 中初始化它) 注释
您应该将 MKAnnotationView 添加到可变数组本身,就在 annView addObserver 代码之前:
然后,在您的 viewDidDisappear 方法中,您可以迭代该数组并手动删除所有观察者:
I know that it's quite old but I see that this code is used a lot on stackoverflow and in other repositories, and here is a solution to the problem.
You should create an NSMutableArray ivar in your view controller class in order to store a reference to your annotations view:
}
Initialize it in your viewDidLoad and in your
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation
you should add the MKAnnotationView to the mutable array itself right before the annView addObserver code:
Then, in your viewDidDisappear method you can iterate the array and remove manually all the observers: