an access violation writing location error the third time when calling a python function in c code

0

I am coding in c to solve a question. In the c code, I need to call a self-defined python to solve a equation. I am able to call the python function for twice time, however when i call the third time, I get an error:"Unhandled exception at 0x00007FEDF8A4E0C(multiarray.cp35-win_amd64.pyd) in Project.1.exe: 0xC0000005: Access violation writing location 0x000000000000000A." It is very strange because in the first and second time, I can call the function successfully and if I independently call the function with the parameters used in the third time, the function can run very well. However, if I try to do the work continuously, it causes the exception above. I have seen a similar question 'C's Python API: Calling python function 2X causes Access violation writing location' in STACKOVERFLOW, however, there is no answer for that problem.

PyObject* pRet = PyEval_CallObject(pv, args);

The c code above is the place where causing the exception when calling the python function the third time. The whole process calling the python function is described more clearly below.

Here comes the python function first

from sympy import *
init_printing(use_unicode=True)
def evaluate_dH(eps,bmax,H,B_H1,B_H2):
    x=symbols("x")
    expr=eps*(x-bmax)**2*x**(H+2)-(H+2)*x*B_H1+(H+1)*B_H2
    result=solve(expr,x)#result is a list type

When solving out the equation, I used two condition that ">bmax" and "not a complex" to find the value in 'result' I need. the final is only one. Thus,

return result[0]

Second, here comes the calling process in c code. Firstly, "double find_dH" is a c function I used in my main function.

double find_dH
(....)
{
double B_H2 = findBH(region, vecp_vertex, H + 2, index);
double B_H1 = findBH(region, vecp_vertex, H + 1, index);
double eps = 0.01;
double bmax = findbmax(region, vecp_vertex);
Py_Initialize();
PyObject *pModule = NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pModule = PyImport_ImportModule("evaluate_dH");
PyObject* pv = PyObject_GetAttrString(pModule, "evaluate_dH");
PyObject* args = PyTuple_New(5);
PyTuple_SetItem(args, 0, Py_BuildValue("d", double(eps)));
PyTuple_SetItem(args, 1, Py_BuildValue("d", double(bmax)));
PyTuple_SetItem(args, 2, Py_BuildValue("i", H));
PyTuple_SetItem(args, 3, Py_BuildValue("d", double(B_H1)));
PyTuple_SetItem(args, 4, Py_BuildValue("d", double(B_H2)));
PyObject* pRet = PyEval_CallObject(pv, args);
double result = PyFloat_AsDouble(pRet);
Py_Finalize();
return result;
}

Here is the place where causing the exception:"Unhandled exception at 0x00007FEDF8A4E0C(multiarray.cp35-win_amd64.pyd) in Project.1.exe: 0xC0000005: Access violation writing location 0x000000000000000A.". But, "PyEval_CallObject(pv, args)" works well before the third time.

PyObject* pRet = PyEval_CallObject(pv, args);

I guess the exception is caused by the reason that: 1. import module 'sympy' multitimes 2. no free for those 'PyObject's in the calling process after each calling.

thanks for your reading and answers!

python
c++
asked on Stack Overflow Jun 15, 2018 by haibing • edited Jun 15, 2018 by haibing

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0