Heap cleanup in python extension

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top