0

I have python codes ebmedded into C++. Do I need to release memory(Py_XDECREF) PyObject* pValue and PyObject *pArgs?

When I do Py_XDECREF(pArgs) and Py_XDECREF(pValue) I have Segmentation Fault (Core dumped). I think python side is still using those variables and c++ try to release memory. What is the best practice for this issue?

for(int i=0; i < 100: i++){
 .......do sth.......
 if (pModule != NULL) {
 std::string st = jps.updateZone(worldx_y, lenVect); 
 PyObject* pValue = PyBytes_FromString(st.c_str());
 if (pFunc_insert && PyCallable_Check(pFunc_insert)) {
 PyObject *pArgs = PyTuple_New(1);
 PyTuple_SetItem(pArgs, 0, pValue);
 PyObject_CallObject(pFunc_insert, pArgs); 
 Py_XDECREF(pArgs); 
 } 
 Py_XDECREF(pValue); 
 }
 ......do sth.......
}
asked Sep 4, 2020 at 2:05
0

1 Answer 1

1

PyTuple_SetItem steals a reference to the item. You don't need to decref the item, because you no longer own a reference to it. You do need to decref the tuple.

If you still get segfaults after that, you have some other bug.

answered Sep 4, 2020 at 2:19
Sign up to request clarification or add additional context in comments.

1 Comment

You meant I do not need for both pValue and pARGs?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.