[wxPython-dev] Problem with MenuItem.Destroy()

Robin Dunn robin at alldunn.com
Fri Dec 22 14:55:59 PST 2006


Paul McNett wrote:

> But the main question on my mind now is: what changed in wxPython 
> 2.7/2.8 versus 2.6 that didn't show these problems?
> 

I switched to a newer version of SWIG that started spitting out the "no 
destructor found" warnings when proxy objects were GC'd but it didn't 
have a C++ destructor (that it knew about) to call.  In 97% of the cases 
that wasn't a problem because the object ownership gets transfered to a 
parent object or something, but I decided that it was time to start 
really using SWIG's built-in object ownership functionality since it was 
now possible to do all the things I needed with it.  So now instead of 
assuming that some kinds of objects will always be owned by some other 
object eventually, they start out being owned by the Python proxy, and 
then relinquish that ownership when the object is passed to some method 
of the other object.  This way if you never pass the item to the other 
object then both the proxy and the C++ object will get GC'd as you would 
expect when your last reference to it is gone.  The only trick to this 
change was identifying the places where ownership is transferred one way 
or the other, classes where Destroy() should become a nop, and corner 
cases like RemoveItem where some special casing needs to be done.

If you ever need to know or manipulate the current ownership status of 
the proxy object then you can use the methods of the .this attribute 
that all swigged objects have.  For example, my fix for RemoveItem is to 
change the method in the proxy class that looks like this:

         def RemoveItem(self, item):
             """RemoveItem(self, MenuItem item) -> MenuItem"""
             # The return object is always the parameter, so return that
             # proxy instead of the new one
             val = _core_.Menu_RemoveItem(self, item)
             item.this.own(val.this.own())
             val.this.disown()
             return item


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





More information about the wxpython-dev mailing list