Segmentation fault in PyArray_SimpleNewFromData
I want to pass an array from C ++ to Python using C-API. While researching various topics here, I learned that I should use the PyArray_SimpleNewFromData method. When I try to implement on a very small array, I get a segmentation fault in my code that I cannot detect. Can anyone help me with this problem?
C ++ code:
void init_numpy()
{
import_array();
}
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs, *pXVec, *xarr1;
PyObject *c ;
PyObject *pValue1 ;
int fArray[2] = {10,1} ;
PyObject *p = NULL ;
npy_intp m1 = 2;
Py_Initialize();
PySys_SetArgv(argc, argv);
init_numpy();
pName = PyString_FromString(argv[1]);
pModule = PyImport_Import(pName);
printf("check0\n");
pDict = PyModule_GetDict(pModule);
printf("check1\n");
pFunc = PyDict_GetItemString(pDict, argv[2]);
printf("check2\n");
c = PyArray_SimpleNewFromData(1,&m1,NPY_INT,fArray);
printf("check3\n");
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs,0,c);
pValue = PyObject_CallObject(pFunc, pArgs);
if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
Python code:
import numpy as np
import scipy.io
def main(a):
print a
Conclusion in detail:
check0
check1
check2
Segmentation fault (core dumped)
+3
source to share
No one has answered this question yet
See similar questions:
or similar: