Hi All,
I have read Lorenzo Sanchez's excellent tutorial on
subclassing GObjects, but am having problems
unreferencing Python objects that have been connected
to GObjects.
The problem simply stated is, if I connect object B to
a signal from object A, I cannot destroy object B
unless I explicitly disconnect the signal handler from
object A first. Having to do this kind of
book-keeping spoils some of the benefits of using a
memory managed language.
I know that the underlying C library uses Closures to
allow the reference count for object B to go to zero;
however, I can't see how this could work in Python
since the language has its own built-in mechanism for
reference counting.
I have come up with the hack below that uses
weakref's; however, this doesn't allow me to take
advantage of the property and signal patterns that I
am looking for which are built into GObject.
Can anyone tell me if I am overlooking something
really obvious with GObject or if there is a better
way of doing this?
Thanks
Bob
import weakref
class A:
d = weakref.WeakKeyDictionary()
def emit(self):
for k, v in self.d.iteritems():
v(k)
def connect(self, client, callback):
self.d[client] = callback
def disconnect(self, client):
self.d.pop(client)
class B:
def __init__(self, a):
a.connect(self, B.my_callback)
def __del__(self):
print 'deleting', self
def my_callback(self):
print 'inside callback'
a = A()
b = B(a)
print 'emit'
a.emit()
print 'done'
print '\nunref b'
b = None
print 'done'
print '\nemit'
a.emit()
print 'done'
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/