Heap cleanup in python extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Eisenhuth

    #1

    Heap cleanup in python extension

    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,flo at] int:[float,float,flo at], ...}}

    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_FromDou ble(1.0);
    PyList_Append(a LedList, 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(aNe wToolDict, &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(aN ewToolDict) 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
Working...