Fixed a bug that crashes the blenderplayer after switching blendfiles a couple of times in the same run.
The problem?
When I took a look at the blender game engine at some point I noticed that the blenderplayer crashed when calling function Py_Finalize() in KX_PythonInit.cpp. So I went to search the internet for an answer... Why does the blenderplayer crash by calling Py_Finalize()?
The answer can be found on https://docs.python.org/3.4/c-api/init.html.
quote from the official Python API:
"Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once."
Since the playerplayer calls Py_Initialze() and Py_Finalize() every time you switch between a level/blendfile, this could be a problem.
The solution?
The solution looks simple. Just get rid of Py_Finalize() and we're done. Unfortunately this caused the sound in our game to get distorted after switching to another blendfile/level. Therefore, I added a new method sound_exit_ghost() (instead of sound_exit()) that deletes the current AUD_IDevice stored in a shared_ptr. This is called in the StopGameEngine() function. Then in StartGameEngine() function we simply call sound_init() again.
I hope someone is able to review my code so that this sneaky bug will be gone for good.
Cheers!
Christiaan, intern gameprogrammer @ Blue Label Studio