I am having trouble embedding Python in C++. I am using Mingw w64 gcc and 64 bit Python 2.7.11.
#include <Python.h>
int main(int argc, char *argv[]) {
Py_Initialize();
PyObject* pName = PyString_FromString("test");
Py_DECREF(pName);
Py_Finalize();
return 0;
}
Calls to compiler:
g++ "-IC:\\Python27\\include" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "src\\main.o" "..\\src\\main.cpp"
g++ "-LC:\\Python27\\libs" -std=c++11 -o pytest.exe "src\\main.o" -lpython27
The problem is that it segfaults in Py_DECREF. I have tried expanding the macros, and have traced segfault to the following statement:
((*(((PyObject*) ((PyObject *) (pName)))->ob_type)->tp_dealloc)((PyObject *) ((PyObject *) (pName))));
Turns out, tp_dealloc points to 0x1.
The same problem happens in the example code provided in Python docs: https://docs.python.org/2/extending/embedding.html#pure-embedding
If I remove some of the calls to Py_DECREF(pName) and Py_DECREF(pArgs), code from the docs works as intended. Yet every example I've found on the web (including the one from the Python docs) does call Py_DECREF.
What could be the cause of this error? Could there be some inconsistency in my build environment?