我在为一件事挣扎。我试图通过注入的dll从python api调用游戏函数。它有时可以工作,但通常会抛出一个错误:
Exception thrown at 0x1E07F731 (python27.dll) in Soria2.pl.exe: 0xC0000005: Access violation writing location 0x0000000C
以上是来自调试器的日志
我的代码:
std::vector<int> mobList()
{
PyObject* mod = PyObject_GetAttrString(PyImport_AddModule("player"), "GetCharacterDistance");
PyObject* mod2 = PyObject_GetAttrString(PyImport_AddModule("chr"), "GetInstanceType");
PyObject* args = PyTuple_New(1);
std::vector<int> mobs;
for (int i = 1; i < 100000; i++) {
try {
PyTuple_SetItem(args, 0, PyInt_FromLong(i));
PyObject* mob = PyObject_CallObject(mod, args);
if (PyInt_AsLong(mob) > 0 && PyInt_AsLong(mob) < 400) {
PyObject* enemy = PyObject_CallObject(mod2, args);
if (PyInt_AsLong(enemy) == 0) {
mobs.push_back(i);
}
if (enemy != NULL) {
Py_DECREF(enemy);
}
}
if (mob != NULL) {
Py_DECREF(mob);
}
Py_XDECREF(args);
}
catch (int e) {
std::cout << e << std::endl;
}
}
return mobs;
}
问题部分:
PyObject* mob = PyObject_CallObject(mod, args);
它有时可以工作,但在随机的迭代量(50k、60k、70k等)之后,大多数情况下会抛出上述错误。 我什么都试过了,但还是想不出来。谢谢你的帮助
我认为@chi是对的:
您应该在for
循环之后Py_XDECREF(args)
。重新阅读文档时,我看到了一个示例:
带着明确的注释:
因此,正如您在一条评论中所说,您可以将
args
创建移动到循环中相关问题 更多 >
编程相关推荐