Hi,

I'm trying to understand how I should cope with gobject deletion. The simple
example that I attach [1] shows a micro application (60 lines) composed of

  * 2 toplevel windows: main and win2

  * a signal emitter (GObject derived) that emits a signal called
    'value-set' each time I press the button from main window

Win2.__init__ connects a callback (a method) from the signal of the emitter.
If I distroy Win2, the callback continues to be called and clearly you see
that Win2 is not deleted.

My comprehension is that the callback is referenced by the emitter and that
is sufficient to keep it alive. Clearly my wish would be that destroying
Win2 (the gtk.Window) could be enought to break Win2 (my custom widget).

What I'd like is to have a connect that only behaves as a weakref!


This should be the key of some memory leaks I have, but I don't know which
should be the best practices to follow when using GObject and signals and I
didnt't find any documentation on this issue.


thanks in advance
sandro
*:-)


[1] http://dpaste.com/hold/402766/
import weakref

import gobject
import gtk

class Emitter(gobject.GObject):
    __gsignals__ = {
         'value-set': (gobject.SIGNAL_RUN_FIRST,
                       gobject.TYPE_NONE,
                       (gobject.TYPE_PYOBJECT, )),
         }
    def __init__(self):
        self.__gobject_init__()

class Win(object):

    def __init__(self, emitter):

        w = gtk.Window()
        self.label = gtk.Label('Win1')
        self.butt = gtk.Button('Emit')
        v = gtk.VBox()
        w.add(v)
        v.add(self.label)
        v.add(self.butt)
        w.show_all()
        w.resize(200, 200)
        self.emitter = emitter
        self.butt.connect('clicked', self.clicked_cb)

    def clicked_cb(self, butt):
        import gc
        gc.collect()
        self.emitter.emit('value-set', 'ok')
        print w2()

class Win2(object):

    def __init__(self, emitter):

        w = gtk.Window()
        label = gtk.Label('Win2')
        v = gtk.VBox()
        w.add(v)
        v.add(label)
        w.show_all()
        w.resize(200, 200)
        #emitter.connect('value-set', weakref.proxy(self.value_set_cb))
        emitter.connect('value-set', self.value_set_cb)
        self.w = weakref.ref(w)
        
    def value_set_cb(self, emitter, value):
        print self.w
        print "value set received by Win2"

emitter = Emitter()
w = Win(emitter)
w2 = weakref.ref(Win2(emitter))


gtk.main()
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to