From d13c51945a07240f174ae3dbb18b56f7c356a58d Mon Sep 17 00:00:00 2001 From: Porteries Tristan Date: Sun, 22 Jan 2017 16:39:06 +0000 Subject: [PATCH] UPBGE: Fix global dict double decref and sub modules incref. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the global dict (stored in bge.logic) was add to the next bge.logic module in case of game restart in LA_Launcher::ExitEngine. But this action is useless and dangerous. First it is dangerous because the globalDict in GPG_ghost could be decref an then delete before calling ExitEngine. Secondly this is useless because the global dict is reset at each call of setupGamePython. So the decref of global dict is removed in ExitEngine. In the same time an extra incref was procceed in initBGE for sub modules. The call to PyModule_AddObject was not incref but the call to PyDict_SetItemString was incref. So no need of manually incref. This incref is removed ant the sub modules add functions are moved into addSubModule(…). Orignal works of youle. Fix issue #350. --- source/gameengine/Ketsji/KX_PythonInit.cpp | 68 +++++++--------------- source/gameengine/Launcher/LA_Launcher.cpp | 1 - 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 1fb87e3ac7af..ccac42707bd6 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -1961,54 +1961,26 @@ static struct PyModuleDef BGE_module_def = { NULL, /* m_free */ }; -PyMODINIT_FUNC initBGE(void) -{ - PyObject *mod; - PyObject *submodule; - PyObject *sys_modules = PyThreadState_GET()->interp->modules; - const char *mod_full; - - mod = PyModule_Create(&BGE_module_def); - - /* skip "bge." */ -#define SUBMOD (mod_full + 4) - - mod_full = "bge.app"; - PyModule_AddObject(mod, SUBMOD, (submodule = initApplicationPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.constraints"; - PyModule_AddObject(mod, SUBMOD, (submodule = initConstraintPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.events"; - PyModule_AddObject(mod, SUBMOD, (submodule = initGameKeysPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.logic"; - PyModule_AddObject(mod, SUBMOD, (submodule = initGameLogicPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.render"; - PyModule_AddObject(mod, SUBMOD, (submodule = initRasterizerPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.types"; - PyModule_AddObject(mod, SUBMOD, (submodule = initGameTypesPythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - - mod_full = "bge.texture"; - PyModule_AddObject(mod, SUBMOD, (submodule = initVideoTexturePythonBinding())); - PyDict_SetItemString(sys_modules, mod_full, submodule); - Py_INCREF(submodule); - -#undef SUBMOD +static void addSubModule(PyObject *modules, PyObject *mod, PyObject *submod, const std::string& modname) +{ + /* PyModule_AddObject doesn't incref the sub module but PyDict_SetItemString increfs + * the item set. So no incref and decref are needed here. */ + PyModule_AddObject(mod, modname.substr(4).c_str(), submod); + PyDict_SetItemString(modules, modname.c_str(), submod); +} + +PyMODINIT_FUNC initBGE() +{ + PyObject *modules = PyThreadState_GET()->interp->modules; + PyObject *mod = PyModule_Create(&BGE_module_def); + + addSubModule(modules, mod, initApplicationPythonBinding(), "bge.app"); + addSubModule(modules, mod, initConstraintPythonBinding(), "bge.constraints"); + addSubModule(modules, mod, initGameKeysPythonBinding(), "bge.events"); + addSubModule(modules, mod, initGameLogicPythonBinding(), "bge.logic"); + addSubModule(modules, mod, initRasterizerPythonBinding(), "bge.render"); + addSubModule(modules, mod, initGameTypesPythonBinding(), "bge.types"); + addSubModule(modules, mod, initVideoTexturePythonBinding(), "bge.textures"); return mod; } diff --git a/source/gameengine/Launcher/LA_Launcher.cpp b/source/gameengine/Launcher/LA_Launcher.cpp index 63e558b51124..b808d08e229d 100644 --- a/source/gameengine/Launcher/LA_Launcher.cpp +++ b/source/gameengine/Launcher/LA_Launcher.cpp @@ -328,7 +328,6 @@ void LA_Launcher::ExitEngine() */ PyDict_Clear(PyModule_GetDict(m_gameLogic)); - PyDict_SetItemString(PyModule_GetDict(m_gameLogic), "globalDict", m_globalDict); #endif // WITH_PYTHON