32

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.

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?

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?

Community
  • 1
  • 1
Kaom Te
  • 774
  • 1
  • 8
  • 17

4 Answers4

46

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.)

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.

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.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • Another option is to create a strong reference to the object being observed in the object observing it. This way the observed object won't be released until the observer is also released. – Avario Dec 10 '15 at 22:47
2

Are you calling [super dealloc] before you remove your observers? Calling super's dealloc too early could lead to an error like this.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • Nope, I definitely have the order right. [super dealloc] is the last thing I call in my dealloc. – Kaom Te Jun 13 '09 at 15:39
  • Old thread but just came across it. Yep, super dealloc definitely should be last. – Ben Dec 06 '10 at 23:32
0

why would you call

[super dealloc]

From apple documentation on dealloc

In an implementation of dealloc, do not invoke the superclass’s implementation
N3al
  • 171
  • 1
  • 6
  • 1
    You do not need to call it only when You use ARC, see this question: http://stackoverflow.com/questions/9058428/documentation-about-dealloc – Mateusz Szlosek Mar 16 '15 at 12:10
-11

The normal code looks something like this:

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

Double check your signature of your dealloc method (Objective C is very unforgiving and will never warn you when you mess up the name of a method). For example, if your method name was "dealoc" (with one l), your dealloc would never be called.

Otherwise, edit your question to include your dealloc reoutine.

Peter N Lewis
  • 17,664
  • 2
  • 43
  • 56