So, I want to call a Python callback function from C.
At some point, the function is sent to C and packed into a tuple like this
PyObject *userData = Py_BuildValue("Oi",py_callback,some_number);
Somewhere in that area, I do Py_INCREF(py_callback), too.
At some later time in the program, I want to call that function
PyObject *py_callback;
int some_number;
PyArg_ParseTuple((PyObject*)userData,"Oi",&py_callback,&some_number); // returns true
PyObject *py_result = PyObject_CallFunctionObjArgs(py_callback,
/* ... */
NULL);
and that last call throws a segmentation fault. Do you have any idea, why it would do such a thing?
When getting weird behaviour from the Python C API, it is always worth double checking that you are managing the state of the Global Interpreter Lock (aka “the GIL”) correctly: http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock