Skip to content

Commit

Permalink
UPBGE: Fix global dict double decref and sub modules incref.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
panzergame committed Jan 22, 2017
1 parent 3721f98 commit d13c519
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 49 deletions.
68 changes: 20 additions & 48 deletions source/gameengine/Ketsji/KX_PythonInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

This comment has been minimized.

Copy link
@panzergame

panzergame Jan 23, 2017

Author Contributor

I'm dumb :S textures -> texture……


return mod;
}
Expand Down
1 change: 0 additions & 1 deletion source/gameengine/Launcher/LA_Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

7 comments on commit d13c519

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow and a clean up! super!

@youle31
Copy link
Collaborator

@youle31 youle31 commented on d13c519 Jan 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an issue with that commit whereas I had not issue with my implementation (by just commenting Py_INCREF) in water demo. All is rendered white. Error in the console about texture module

@panzergame
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youle31, can you paste the console output ?

@panzergame
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 58744db.

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok I check

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right :)

Please sign in to comment.