Where should I put Py_INCREF and Py_DECREF on this block in Python C Extension?

前端 未结 2 1977
囚心锁ツ
囚心锁ツ 2021-01-05 06:36

Whenever I called my function, memory usage is increased around +10M per call, so I think there is some memory leak here.

....
PyObject *pair = PyTuple_New(2         


        
2条回答
  •  猫巷女王i
    2021-01-05 07:02

    The objects you create with PyInt_FromLong() and you add to the list should be kept in a local variable.

    The reason are the ownership rules: PyInt_FromLong() generates a reference that you own. In the call to PyTuple_SetItem(), you lose this ownership again, because PyTuple_SetItem() "steals" it from you, so you don't have to care about. But PyList_Append() doesn't do so, it increases the refcount. In order to have the object GC'ed correctly, you have to release your ownership by DECREF'ing.

    So, instead of PyList_Append(item, PyInt_FromLong(jp)), you do the following:

    PyObject * jpo = PyInt_FromLong(jp);
    // do some error checking here
    PyList_Append(item, jpo);
    Py_DECREF(jpo);
    

    This will make the program do the right thing.

提交回复
热议问题