[wxPython-users] __del__ not called ?

Robin Dunn robin at alldunn.com
Fri Feb 1 08:37:19 PST 2008


Stef Mientki wrote:

If you haven't already read it, here is some background info on __del__: 
http://effbot.org/pyref/__del__.htm

> hello,
> 
> I've a class, derived from object,
> that consists of several graphical objects,
> and some interface with an external program.
> Now I have to kill the external program when my program finishes.
> And example of the object is given below.
> 
> In the standalone application, where the class instance was dropped 
> straight on a miniframe,
> I already discovered that closing the miniframe, didn't always generate 
> an onclose event,
> so I found out that in this case the __del__ function worked always 
> correctly.

They're not quite the same thing though.  The EVT_CLOSE event will 
happen before the window is actually destroyed and will give you a 
chance to prevent it from doing so.  The __del__ will happen after the 
window is destroyed, when the Python proxy object is being cleaned up.

BTW, can you provide a sample that shows the miniframe not getting the 
EVT_CLOSE?

> 
> Now if I hang this class instance in my larger program,
> where all instances are generated dynamically and
> are placed on an AUI-panel (alone of together with other objects),
> the __del__ function is never called.
> 
> Why is the __del__ function not called ?

Are you sure there are no other references to the object?  Note that 
widget objects will hold a reference to themselves, (the C++ object has 
a reference to the Python proxy object) and every event binding that a 
widget has to a method of some object will also hold a reference to the 
object.

> 
> class AD_Converter ( object ) :
>    def __init__ ( self,  Dock )    # the frame/panel/... where we can put
>       self.Button_Start = wx.Button ( Dock, -1)
>       self.Button_Start.Bind ( wx.EVT_BUTTON, self.OnStart, 
> self.Button_Start )
> 
>    def OnStart ( self, event ) :
>        .. if external program not yet exists, launch it
>        .. if external program is running, stop it (don't kill it)
>        .. else start it (again)
> 
>    def __del__ ( self ) :
>        .. if external program exists, kill it

So in this sample the button will end up with an extra reference to the 
AD_Converter object because of the event binding to OnStart.  So if the 
button is never destroyed then that reference will remain and the 
AD_Converter's __del__ won't be called.  Normally this won't be a 
problem because when the button's parent window is destroyed then the 
C++ object of the button will be too and those extra references will be 
removed.  Perhaps the problem is that the AUI panes are not being 
destroyed when they are closed, but just hidden?  You can use the widget 
inspector to find out for sure.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list