• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
KVO and dealloc
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

KVO and dealloc


  • Subject: KVO and dealloc
  • From: Jeff Gilbert <email@hidden>
  • Date: Fri, 24 Sep 2004 05:23:59 -0500

Am I making things too complicated here?

I have a document class:

@interface BondDocument : NSDocument
{
NSMutableArray* bonds;
NSMutableArray* groups;
}

groups is a list of Group items which are similar to playlists in iTunes. The Group items use KVO to observe the bonds of the document as in:

- (void)startObservingDocument:(BondDocument*)newDocument
{
[self setDocument:newDocument];


[[self document] addObserver:self forKeyPath:@"bonds" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:BondsArrayObservationContext];
}


In my document dealloc, I do

- (void)dealloc
{
[self setGroups:nil];
[self setBonds:nil];
[super dealloc];
}

setGroups tells each Group object to removeObserver to stop KVO on the document bonds. So, as the document is deallocing, all of the KVO observations from groups are removed. However, as soon as I enter my document dealloc, I get the following message in the Run window:

An instance 0x3418c0 of class BondDocument is being deallocated while key value observers are still registered with it. Break on _NSKVODeallocateLog to start debugging.

Then, I quickly get a signal 11.

I realize that there are still observers, but I plan to remove them during dealloc.

I can solve the problem by moving [self setGroups:nil] to the close method, as in

- (void)close
{
// For some reason, when we get to dealloc, there cannot be any KVO observers attached to the document.
// Since the items in groups will be observing the document for bond changes, we need to detach the groups
// from the document before the document is dealloc'ed. While the document is closing seems to be the best time.
[self setGroups:nil];


[super close];
}

Following the Cocoa mantra "Things are simpler than they seem", it seems that this is too complicated, in that it breaks from the tradition of releasing everything in dealloc. Or am I just being too sensitive? Should I just move all of my dealloc stuff to close?

thanks,
Jeff Gilbert
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: KVO and dealloc
      • From: DumberThanDanQuayle <email@hidden>
  • Prev by Date: Regarding Font style in an NSTextView
  • Next by Date: Re: KVO and dealloc
  • Previous by thread: Re: Regarding Font style in an NSTextView
  • Next by thread: Re: KVO and dealloc
  • Index(es):
    • Date
    • Thread