Hi everyone,

I maintain some code that does dynamic subclassing to override an object's 
-dealloc method to do some extra cleanup prior to deallocation.  (And for the 
curious, this cleanup is not necessary when using Garbage Collection)

However, I observed a problem this morning, and that's when I try to 
dynamically subclass an object that has some observers attached.  Something is 
going on inside the KVO mechanism that apparently can't handle the class of the 
object not being the NSKVONotifying_* class.  (Or perhaps it can and I just 
haven't overridden the necessary stuff)

I tried adding an overridden -class method to my dynamic subclass which would 
simply return [super class] (since NSKVONotifying_* classes appear to be hiding 
their class as well), but that also did not work.

I considered doing something like inserting my subclass as the superclass of 
the NSKVONotifying_* class, but the documentation for class_setSuperclass() 
says "You should not use this function.", so I figured that I should probably 
not use this approach.

The problem would manifest itself when I had an object observing one of its own 
synthesized properties.  If that object had been dynamically subclass by my 
code *after* registering itself as an observer, then I would starting getting 
unrecognized selector crashes from inside the _NSSetObjectAndNotify function of 
the KVO'd setter.  These crashes were usually something like an unrecognized 
selector of "+[NSCFString objectForKey:]".

If I made sure that my subclassing happened before adding observers, then 
things would work properly.

However, trying to enforce that subclassing happens before observation 
registration would require some significant refactoring of a dependent code 
base, which I don't have time for (we've entered our internal beta testing 
period).  So currently what I've done is added a custom -dealloc method to the 
existing class (whether it's an NSKVONotifying_* class or not), then exchange 
that dealloc method with the original one.  The end result of this is that when 
the object receives the -dealloc message, it invokes the new method, which then 
forwards on to the original dealloc method, which should then forward things on 
to super.

This, however, seems a bit more fragile than the dynamic subclassing approach, 
and curiosity has gotten the better of me.  So my question is:

What's involved with safely subclassing an object that's already been 
dynamically subclassed with KVO?

Thanks,

Dave

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to