Re: Listener related crashing bug releasing subviews of a custom view
Re: Listener related crashing bug releasing subviews of a custom view
- Subject: Re: Listener related crashing bug releasing subviews of a custom view
- From: glenn andreas <email@hidden>
- Date: Fri, 22 Feb 2008 11:08:35 -0600
[ attempting to cc to Cocoa Dev where it is more on-topic than Xcode
dev]
On Feb 22, 2008, at 10:22 AM, Ken Tozier wrote:
Thanks for the tips Martin. (I particularly like [self
observationInfo]. Very helpful)
I tried both your suggestions and wrote a loop in the dealloc method
to force "[model removeObserver: self forKeyPath: @"pageNumber"];"
on all subviews but still no luck.
I rewrote the dealloc method to perform the remove observer step
directly and got the following call stack on crash
- (void) dealloc
{
NSLog(@"Entered: PMPageNumberField:dealloc");
[model removeObserver: self forKeyPath: @"pageNumber"];
[super dealloc];
}
Here's the problem (and, IMHO, design flaw in KVO). By the time your
dealloc method is called, the KVO "something still registered" check
has already happened, fired off, and complained (it should be made
afterwards - such as in the NSDeallocateObject). As a result, this
means that you need to unbind the listeners _before_ your dealloc
method is called - and that's why your removeObserver in dealloc
didn't help. This, of course, is a bit of a chicken-and-egg problem
(you don't know that the last reference to you has been released until
you get to dealloc, but by that time it's too late, but you can't know
to unbind before then in all cases - too bad something doesn't keep
track if an object is allocated or not - oh, wait, it does, and calls
dealloc when it is no longer referenced).
For a document based apps, it's not too hard (you can unbind things in
the close method). For other things, you'll need to add some other
sort of magic (I've not tried it, but you could trying overriding
release, calling super, and seeing if the release count dropped to
zero, and if so, unbind - though to be honest, something about that
feels wrong, and certainly won't work for GC, but finalize may work
for that purpose on GC).
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next
generation of fractal art
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden