[Pyobjc-dev] weakref-like behavior on ObjC subclasses
Brought to you by:
ronaldoussoren
|
From: James R E. <ea...@ba...> - 2008-02-25 23:27:03
|
I have a class that implements an observer pattern in python. Observables maintain weakrefs to listeners that have been registered with them such that, when a listener goes out of scope, the observable doesn't keep it alive. Of course, this mechanism fails as soon as a listener inherits from an ObjC class because weakref doesn't support ObjC classes. At this point, my workaround is to store a strong reference if the listener can't be weakref'd. I'm not happy about this solution because it now creates different behaviors depending on whether the object can be weakref'd. If it can, then the listener is automatically unregistered from the observable when its lifespan is up. If it can't, however, then the object must be manually unregistered before deletion will ever occur. This is where the problem arises in PyObjC. Is there any mechanism through which I can approximate a weak reference to an ObjC object in Python? I tried doing some magic by checking the retainCount() of my objects, but that seems to always be the same, no matter how many Python objects hold a ref to the ObjC instance. Thanks in advance! James PS: I'd love to be able to use an NSHashTable with NSHashTableZeroingWeakMemory, but I need to maintain 10.4 support on this project. :-( -- Beauty is in the eye of the beer holder... |