Heap cleanup in python extension

Alexander Eisenhuth newsuser at stacom-software.de
Wed Nov 15 18:21:46 CET 2006


Hello Together,

Shortly what I'm  doing:
- Extending python with boost.pthon extension
- Using python C-Api for datatypes in the extension
- extension has a thread (that can be stopped/started)
- thread started: extension updates a dict (given as parameter to the extension) 
every 20 ms
- the dict looks like:
{int:{int:[float,float,float] int:[float,float,float], ...}}

Now my question:
I create the dict every 20 ms on the heap in the following way:
------------------------------
static PyObject*  createPTDict()
{
	static int aNumIter = 0;

	PyObject* nRetDictPy =  PyDict_New(); // dicto to return
	PyObject* nToolIdPy = PyInt_FromLong(1234567);
	PyObject* nLedDict =  PyDict_New(); // dict containing 5 key,values
		
	for (int aLedNum=0; aLedNum < 5; aLedNum++)
	{
		PyObject* aLedNumPy = PyInt_FromLong(aLedNum);
		PyObject* aLedList = PyList_New(0);
		for ( int aXYZ=0; aXYZ < 3; aXYZ++)
		{
			PyObject* aLedPosXYZ = PyFloat_FromDouble(1.0);
			PyList_Append(aLedList, aLedPosXYZ);
		}
		PyDict_SetItem(nLedDict, aLedNumPy, aLedList);
	}
	PyDict_SetItem(nRetDictPy, nToolIdPy, nLedDict);
	aNumIter ++;
	return nRetDictPy;
}
------------------------------
I give it to python with:
------------------------------
PyObject *aKey, *aValue;
int aPos = 0;
PyObject* aNewToolDict = createPTDict();
PyDict_Next(aNewToolDict, &aPos, &aKey, &aValue);

// DictInPython passed prior to extension
PyDict_SetItem(<DictInPython>, aKey, aValue);
------------------------------
And now, what would be a proper way to cleanup the heap?
1) Does PyDict_Clear(aNewToolDict) also decrement the ref counting for the 
containing list, int, ... PyObjects?
2) Do I need to Py_CLEAR( PyObject *o) for every created PyObject?

Thanks a lot for every answer/hint.

Regards
Alexander



More information about the Python-list mailing list