gpt4 book ai didi

cocoa - 使用 KVO 时出现异常

转载 作者:行者123 更新时间:2023-12-03 17:32:45 26 4
gpt4 key购买 nike

我正在实现基于 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>

最佳答案

It is working as excepted, but some time it is getting exception 'EXC_BAD_ACCESS'. My log is as follows and it is showing me a leaking memory. …

An instance 0x1b21f0 of class MKAnnotationView is being deallocated while key value observers are still registered with it.

这与泄漏相反。它正在被释放;泄漏是指对象永远不会被释放。

问题在于它正在被释放,而其他东西仍在观察它。任何仍在观察该对象的对象也可能稍后向其发送其他消息;当它发生时,这些消息将发送到一个死亡对象(导致您看到的崩溃,发生在该消息之后)或发送到另一个对象。

如果观察 MKAnnotationView 的对象拥有它并释放它,则在释放它之前需要将自己作为观察者删除。如果它不拥有它,它可能应该拥有它。

关于cocoa - 使用 KVO 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4067299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com