C++嵌入式 Python PyBytes_AsString导致 DLL 崩溃
C++ embedded Python PyBytes_AsString causing DLL to crash
试图让C++和Python嵌入式DLL工作,它似乎绊倒了PyBytes_AsString组件。
具体来说,这一行:
strcpy(buffer, PyBytes_AsString(pValue));
它试图将返回值从 milp_closest.solve 函数 int 复制到 char 缓冲区。 但它几乎使DLL崩溃,就像它们是不兼容的类型一样。
输入格式 - milp_closest.solve(10, 20, 30, 25(
输出字符串格式 - (0, 1, 25.0, [
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1](// The Function calls a Python module named milp_closest()
// Input parameters: 4 integers
// Output parameters: a string return by the call to the Python module
extern "C" LPCSTR __declspec( dllexport ) __stdcall TS2Py_Milp(int n, int p1, int p2, int average)
{
static char buffer[256]={""};
PyObject *pName, *pModule, *pDict, *pfSolve;
PyObject *pArgs, *pValue;
Py_Initialize();
//pName = PyString_FromString("");
/* Error checking of pName left out */
pModule = PyImport_ImportModule("milp_closest"); // which Python module do we wish to load
//Py_DECREF(pName);
if (pModule != NULL)
{
pfSolve = PyObject_GetAttrString(pModule, "solve"); // which function in the above-loaded module, do we wish use
/* pfSolve is a new reference */
if (pfSolve && PyCallable_Check(pfSolve))
{
pArgs = PyTuple_New(4); //The Solve funciton takes 4 input parameters
pValue = PyLong_FromLong((long) n); // store 1st parameter (n) for function Solve
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyLong_FromLong((long) p1); // store 1st parameter (n) for function Solve
PyTuple_SetItem(pArgs, 1, pValue);
pValue = PyLong_FromLong((long) p2); // store 1st parameter (n) for function Solve
PyTuple_SetItem(pArgs, 2, pValue);
pValue = PyLong_FromLong((long) average); // store 1st parameter (n) for function Solve
PyTuple_SetItem(pArgs, 3, pValue);
pValue = PyObject_CallObject(pfSolve, pArgs); // call the Python funciton "Solve"
Py_DECREF(pArgs);
if (pValue != NULL)
{
strcpy(buffer, PyBytes_AsString(pValue));// copy the RERTURN value(string) form Python function call, in to our return value
Py_DECREF(pValue);
}
else
{
Py_DECREF(pfSolve);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failedn");
return "call to function solve() failed";
}
}
else
{
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function "solve"n" );
}
Py_XDECREF(pfSolve);
Py_DECREF(pModule);
}
else
{
PyErr_Print();
fprintf(stderr, "Failed to load "milp_closest.py"n");
return "Failed to laod module <milp_closest.py>";
}
Py_Finalize();
// we return (or pass on) the string returned from the call to the Python function "solve".
return (LPCSTR) buffer;
关于我可能出错的地方有什么建议吗?
首先,确保您知道导致崩溃的确切行(使用 print 语句或通过调试器(。 这将确保我们调试正确的区域。此外,请尝试打印从 PyBytes_AsString(( 返回的 char*。
其次,您将元组传递给 milp_closest.solve()
函数,但您的示例传递了 4 个整数。我想你的意思是这个?
PyObject* args = Py_BuildValue("llll", 10, 20, 30, 25);
if (!*args) {
/* handle error */
}
Py_Object* pValue = PyObject_CallObject(pfSolve, args);
相关文章:
- 函数指针分配在创建 DLL 后导致错误
- Unity3D的OpenCV dll调用导致FPS下降
- DLL 导出导致唯一指针出现问题
- 从 C# Windows 应用程序调用 C dll 会导致 svchost.exe 崩溃
- 在注入的 Dll 的 DllMain 中运行代码导致注入超时
- 从模板继承的 MSVC DLL 导出类会导致LNK2005已定义的错误
- 使用参数的 C# 委托回调会导致从 DLL 调用时C++访问冲突
- 将本机非托管的C DLL加载到托管的C#应用中,导致DLL输出垃圾
- 将std ::向量转换为阵列和当时的p/调用它会导致访问违规例外,在编组期间mscorlib.dll
- 将静态库转换为 DLL 会导致在 main 之前出现访问冲突
- 从 /MD Exe 文件中删除 /MDd 的 DLL 内存会导致堆损坏
- OpenGL应用程序导致d3d11.dll中的堆栈溢出
- DLL 中的 AFX_MANAGE_STATE(AfxGetStaticModuleState()) 会导致 EXE 不
- 由 Boost 单元测试框架 dll 导出的 std::basic_ostringstream 会导致"already defined symbol"错误
- 如果实际导出"ordinal N not found in DLL"错误,可能导致什么原因?
- DLL 中的 C++ 静态局部变量初始化会导致_CrtIsValidHeapPointer异常
- 什么可能导致时间函数和 DLL 加载之间死锁
- 将嵌套结构变量从 C++ 传递到C++ dll 会导致 (L "Buffer is too small" && 0)
- 什么可能导致ntdll.dll中的C++程序崩溃
- C++嵌入式 Python PyBytes_AsString导致 DLL 崩溃