[PyKDE] Memory leak in converstion to std::string & ?

Phil Thompson phil at riverbankcomputing.co.uk
Mon Aug 16 10:35:54 BST 2004


On Friday 13 August 2004 12:53 am, Paul F. Kunz wrote:
>    I have a C++ class with a method
>
>   virtual void setAutoTicks ( const std::string &,  bool );
>
>  I wrote the ConvertToTypeCode this...
>
>
> %ConvertToTypeCode
>          // Allow a Python string whenever a string is expected.
>          if (sipIsErr == NULL)
>              return PyString_Check(sipPy);

Shouldn't you also check if sipPy is a std::string instance?

>          if (sipPy == Py_None) {
>              *sipCppPtr = NULL;
>              return 0;
>          }
>          if (PyString_Check(sipPy)) {
>              *sipCppPtr = new string(PyString_AS_STRING(sipPy));
>              return 0;

This should return 1, not 0.

>          }
>          *sipCppPtr = (string
> *)sipForceConvertTo_std_string(sipPy,sipIsErr); return 1;

This should return 0, not 1.

> %End
>
> It works, but it leak memory.   One of my users used the method in a
> loop so it was called 200,000 times and he reported it leaked about
> 250 bytes per iteration.   Taking out the string argument and
> re-running his test case, showed no leak at all.
>
>    Any ideas?

Basically you are saying the result is on the heap when it isn't, and vice 
versa.

Phil




More information about the PyQt mailing list