On Tue, May 25, 2010 at 08:41:37AM +0200, orcim wrote:
> Alessandro Dentella ha scritto:
> 
>     On Fri, May 21, 2010 at 05:57:00PM +0200, Alessandro Dentella wrote:
> 
> 
>         Hi,
> 
>         I'm trying to figure out the correct way to delete a class that builds
>         GUI.
> 
>         I was not able to delete the object from a callback in a clean way.
> 
>         The following code should demostrate what I mean::
> 
>           import weakref
>           import gtk
>           import gobject
> 
>           class A(object): pass
> 
> 
>           class G(object):
>               def __init__(self):
>                   self.a = A()
>                   self.w = gtk.Window()
>                   self.w.show()
>                   self.w.connect_after('delete-event', self.delete_event_cb)
> 
>               def delete_event_cb(self, win, ev):
>                   print 'killed'
>                   del self
>                   print "s", s()   # I'd like to see this to become None
>                   gtk.main_quit()
> 
>           g = G()
> 
>           s = weakref.ref(g.a)
> 
>           gtk.main()
> 
>         My goal is to create a class 'G'  that will destroy itself completely
>         when the window it holds receives a "delete_event".
> 
>         As written in the comment, I'd like to see that the weakref to an 
> element
>         inside the instance 'g' should be None after deletion of 'g'. But 
> it's not!
> 
>         Deleteing the object prints:
> 
>           san...@bluff: $ python test_mem.py
>           killed
>           s <__main__.A object at 0x8329eec>
> 
>         If I get rid of 'self'in the callback, everithing works but that makes
>         it impossible to put the function inside the class.
> 
>         If I use a delete_event_cb as a separate function I manage to destroy 
> it if
>         I don't pass the g object as argument to the callbac (ie: making it 
> global)
>         but clearly that's a far less usefull setup.
> 
>         What's the best practice to delete compound widgets?
> 
> 
> 
>     Further tests on this showed me that if I  set data on a widget
>     (e.g. using set_data) that contitues a strong reference that will not let
>     the gc to delete the object::
> 
>        import gc
>        import sys
>        import weakref
>        import gtk
> 
>        class G(object):
>            def __init__(self):
>                self.w = gtk.Window()
>                self.w.show()
>                self.w.set_data('obj', self)  # this reference keeps 's' 
> "alive"
>                self.w.connect_after('delete-event', self.delete_event_cb)
>                self.w.connect('enter-notify-event', self.enter_event_cb)
> 
>            def delete_event_cb(self, win, ev):
>                print 'killed'
> 
>            def enter_event_cb(self, win, ev):
>                global s
>                print 'enter', s()
> 
> 
>        s = weakref.ref(G())
>        print s()
> 
>        try:
>            gtk.main()
>        except KeyboardInterrupt:
>            print s()
>            sys.exit(1)
> 
>     I'd say this is a bug, am I wrong? is this a known problem?
>     That clearly leads to memory leaks.
> 
>     Any hints?
> 
>     sandro
>     *:-)
> 
> 
> 
> 
> probably there 'a reference object gtk still alive somewhere
> ciao Loris

Yes, that's exactly what I would consider a bug. I hoped this list could
comment on this as it's a problem that should hit *anybody* writing
application with PyGTK...

I verify that each widow I open increases the amount of RAM needed and in no
may I can decrease it when closing the window. 

I may have done some serious error but I don't find any best practice on
this and I consider this a very important issue.


sandro


-- 
Sandro Dentella  *:-)
http://sqlkit.argolinux.org        SQLkit home page - PyGTK/python/sqlalchemy
_______________________________________________
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