[Bf-python] Memory leak - BPY

Ken Hughes khughes at pacific.edu
Wed Apr 4 22:47:21 CEST 2007


Campbell Barton wrote:
> Hi, take a look at this from Ipocurve, looks like xobj and yobj need to 
> be decrefed,

You're right, they do need a decref.

> A lot of BPythons code uses PyNumber_Float, where its not needed, aside 
> from complicating things by taking a reference, its only bein used to 
> check.
> 
> 
> float f;
> PyObject *(!pyfloat) = PyNumber_Float( pyob );
> if (!pyfloat) .... raise error
> f = (float)PyFloat_AsDouble( pyfloat );
> Py_DECREF( pyfloat );
> 
> 
> Its probably better and less problem prone  to do this.
> 
> float f;
> if (!PyNumber_Check(pyob)) .... raise error
> f = (float)PyFloat_AsDouble( pyob ); /* PyFloat_ASDOUBLE works too I 
> guess */

I started using using PyNumber_Float() as part of the goal to allow 
users to give us anything remotely number-like as input without failing, 
for example

   ob.LoxC = '1.5'
   ob.LocX = True
as opposed to
   ob.LocX = float('1.5')
   ob.LocX = float(True)

(I started doing this back around the discussion of PyObject_IsTrue() 
being used).

If PyFloat_AsDouble() will take non-float objects then your way is 
definitely better; I haven't tried it to be sure.

Ken



More information about the Bf-python mailing list