[Bf-python] Memory leak - BPY

Campbell Barton cbarton at metavr.com
Wed Apr 4 21:21:08 CEST 2007


Hi, take a look at this from Ipocurve, looks like xobj and yobj need to 
be decrefed,

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 */





Heres the IPO curve func I suspect is leaking.



static PyObject *IpoCurve_append( C_IpoCurve * self, PyObject * args )
{
	float x, y;
	IpoCurve *icu = self->ipocurve;
	PyObject *obj = NULL;

	if( !PyArg_ParseTuple( args, "O", &obj ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
				"expected tuple or BezTriple argument" );

	/* if args is a already a beztriple, tack onto end of list */
	if( BPy_BezTriple_Check ( obj ) ) {
		BPy_BezTriple *bobj = (BPy_BezTriple *)obj;

		BezTriple *newb = MEM_callocN( (icu->totvert+1)*sizeof(BezTriple),
				"BPyBeztriple" );
		if( icu->bezt ) {
			memcpy( newb, icu->bezt, ( icu->totvert )*sizeof( BezTriple ) );
			MEM_freeN( icu->bezt );
		}
		icu->bezt = newb;
		memcpy( &icu->bezt[icu->totvert], bobj->beztriple,
				sizeof( BezTriple ) );
		icu->totvert++;
		calchandles_ipocurve( icu );
	
	/* otherwise try to get two floats and add to list */
	} else {
		PyObject *xobj, *yobj;
		xobj = PyNumber_Float( PyTuple_GetItem( obj, 0 ) );
		yobj = PyNumber_Float( PyTuple_GetItem( obj, 1 ) );

		if( !xobj || !yobj )
			return EXPP_ReturnPyObjError( PyExc_TypeError,
				"expected tuple of floats" );

		x = (float)PyFloat_AsDouble( xobj );
		y = (float)PyFloat_AsDouble( yobj );
		insert_vert_ipo( icu, x, y);
	}

	Py_RETURN_NONE;
}




More information about the Bf-python mailing list