[PyKDE] Re: clone member function deleting object

Phil Thompson phil at riverbankcomputing.co.uk
Mon Mar 14 16:54:49 GMT 2005

>>>>>> On Mon, 14 Mar 2005 15:34:04 -0000 (GMT), "Phil Thompson"
>>>>>> <phil at riverbankcomputing.co.uk> said:
>>>>>>>> On Thu, 3 Mar 2005 09:17:07 -0000 (GMT), "Phil Thompson"
>>>>>>>> <phil at riverbankcomputing.co.uk> said:
>>>  Enabling tracing with SIP, I get the following when I call the
>>> clone() member function in C++...
>>> FunctionBase * sipFunctionBase::clone() const (this=0x083b0878)
>>> python.clone
>>> sipFunctionBase::sipFunctionBase(const FunctionBase&) (this=0x0842b2d8)
>>> python.copy
>>> sipFunctionBase::~sipFunctionBase() (this=0x0842b2d8)
>> This isn't very helpful unless it shows your Python debug print
>> statements so that you can see the exact order that things are
>> happening.
>    The Python print statements are prefixed by `python.'

Whoops - missed that.

>> Obviously it will do this if you aren't saving the result of the
>> call to clone().
>    I'm trying to save the results to clone() in C++.

You mean you are not saving a reference to the Python object returned by
clone()? If you are not saving a reference then it will immediately get
garbage collected and, because the underlying C++ instance is owned by
Python then it's dtor will be called.

You must either keep a reference or transfer ownership to C++ using
sip.transfer() - and make sure you call the C++ dtor explicitly at some
stage to avoid a memory leak.

>>> What might I try next?
>> Additional debug statements to see exactly which Python statement
>> the dtor is being called from.
>    As far as I can see, from the above...
> - C++ calls virtual function clone() of the base class (FunctionBase)
> - sipFunctionBase get called, it calls clone() of the Python function.
> - clone of the Python function calls the "copy constructor".
> - sipFunctionBase copy constructor gets called and it calls Python
>   function's __init__ with two arguments
> - Python __init__ prints "python.copy".
> - sipFunctionBase destructor gets called, destroying the newly
>   constructed object.

What you haven't shown is the Python code that calls clone().


More information about the PyQt mailing list