我什么时候应该罢免观察员?关于在删除观察者之前重新分配对象的错误 [英] When should I remove observers? Error about deallocating objects before removing observers

查看:82
本文介绍了我什么时候应该罢免观察员?关于在删除观察者之前重新分配对象的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的一门课中使用键值观察.我在初始化方法中注册了观察者,并在dealloc中删除/注销了观察者,但是根据我的调试打印,我得到了以下错误,该错误似乎在我的dealloc方法被调用之前发生.

I am trying to use key-value observing in one of my classes. I register the observers in the init method and remove/deregister them in the dealloc, but I get the following error which seems to occur before my dealloc method gets called, according to my debug prints.

正在对TekkPoint类的实例0x583870进行释放,同时仍向其注册键值观察器.观察信息正在泄漏,甚至可能被错误地附加到其他物体上.在NSKVODeallocateBreak上设置一个断点以在调试器中停止.这是当前的观察信息: ( 上下文:0x0,属性:0x536400> 上下文:0x0,属性:0x55aba0> )

An instance 0x583870 of class TekkPoint 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: ( Context: 0x0, Property: 0x536400> Context: 0x0, Property: 0x55aba0> )

我应该以某种特定的方式删除观察者吗?或也许是我应该将其移除的特定位置?

Is there a particular way I should remove observers? Or perhaps a particular place that I should be removing them?

根据此

According to this question I am doing things right, but why would I get this error message?

这是我的dealloc例程:

Here is my dealloc routine:

- (void)dealloc {
  // Remove all observers.
  for (NSString *path in [TekkPoint observedPaths]) {
    [self removeObserver:[option_ tChart]
          forKeyPath:path];

  }

  [description_ release];
  [weight_ release];
  [super dealloc];
}

要注意的一件事可能与我的实现方式有关,那就是我要在被观察者中添加观察者或从中移除观察者,这可能会引起我的问​​题吗?

One thing to note that might be strange about my implementation is that I am adding and removing observers from the observee, could this cause my problem?

推荐答案

啊.您正在观察SomethingElse对象中的TekkPoint对象,而SomethingElse对象是添加和删除观察者的对象,对吗? (这是正常的工作方式;我只是想澄清一下.)

Ah. You're observing a TekkPoint object from a SomethingElse object, and the SomethingElse object is the one adding and removing the observers, correct? (That's the normal way things are done; I'm just trying to clarify.)

看来,正在观察的SomethingElse仍在释放您的TekkPoint对象.不会调用SomethingElse dealloc方法,因为它是被释放的是TekkPoint,而不是SomethingElse.

It looks like your TekkPoint object is being deallocated while the SomethingElse that's observing it is still around. The SomethingElse dealloc method isn't called because it's the TekkPoint that's being deallocated, not the SomethingElse.

如果您打算观察一个可能在观察者消失之前消失的物体,那么您需要某种方式通知观察者他们应该移除他们的观察者.您的TekkPoint可能具有alive属性,该属性也会被SomethingElse观察到,并且当它设置为NO时,每个观察TekkPoint的人都会将自己作为观察者移除.

If you're planning on observing an object which may disappear before the observer disappears, then you need some way of notifying the observers that they should remove their observers. Your TekkPoint could have an alive property which would also be observed by the SomethingElse, and when it gets set to NO then everyone observing the TekkPoint would remove themself as an observer.

这篇关于我什么时候应该罢免观察员?关于在删除观察者之前重新分配对象的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆