Re: Deallocation while key value observers still registered
Re: Deallocation while key value observers still registered
- Subject: Re: Deallocation while key value observers still registered
- From: Gideon King <email@hidden>
- Date: Wed, 26 May 2010 03:08:31 +1000
I have found the problem. In awakeFromNib, I have several calls like this:
[self addObserver:self forKeyPath:@"thingy" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:NULL];
And in the dealloc method, I have:
[self removeObserver:self forKeyPath:@"thingy"];
for all the same key paths.
All looked normal enough. But in the init method of the class, I was loading a nib that had my class as the file's owner, as well as being a view controller for another view. Therefore, awakeFromNib was being called twice, and although the dealloc was removing one set of registrations, the other set was being left. In my core data classes, I have all this wrapped in a boolean that avoids starting observing more than once, but I wasn't doing that in this part of my code.
I found the -observationInfo method useful in tracking this down - I knew there must be some method like this, but had not found it before.
Thanks for the suggestions etc.
Gideon
On 26/05/2010, at 2:29 AM, Kyle Sluder wrote:
> On Tue, May 25, 2010 at 9:08 AM, aglee <email@hidden> wrote:
>> I had an idea for a perverse hack to deal with the problem in Leopard, which
>> was to to override release and do the deregistering there if the retainCount
>> is 1, thus sneaking in the deregistering just before dealloc but no sooner.
>
> I believe this was actually impossible because of where the dealloc
> check was implemented (right at the top of -release, IIRC; -dealloc
> didn't actually do the checking, just the work of cleaning up).
>
> --Kyle Sluder
_______________________________________________
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