From 03df7e9a564d1ba6366b5d6b587b9319158ca198 Mon Sep 17 00:00:00 2001 From: Mitchell Stokes Date: Sat, 12 Jan 2013 05:44:08 +0000 Subject: [PATCH] BGE: Fix for [#33839] "a logic.joysticks Crashes BGE" reported by Josiah Lane (solarlune). On a scene change the SCA_JoystickManager gets destroyed which in turn means all of it's joystick instances are released. Since SCA_PythonJoystick was just using a borrowed reference, this allowed the joystick to be freed. Now the joystick's refcount is incremented so that the SCA_PythonJoystick's joystick reference will survive across scene changes. --- source/gameengine/GameLogic/SCA_PythonJoystick.cpp | 4 ++++ source/gameengine/Ketsji/KX_PythonInit.cpp | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/gameengine/GameLogic/SCA_PythonJoystick.cpp b/source/gameengine/GameLogic/SCA_PythonJoystick.cpp index 8c0a0c5ae33..9b24ad7bcf2 100644 --- a/source/gameengine/GameLogic/SCA_PythonJoystick.cpp +++ b/source/gameengine/GameLogic/SCA_PythonJoystick.cpp @@ -46,6 +46,10 @@ m_joystick(joystick) SCA_PythonJoystick::~SCA_PythonJoystick() { + // The joystick reference we got in the constructor was a new instance, + // so we release it here + m_joystick->ReleaseInstance(); + #ifdef WITH_PYTHON PyDict_Clear(m_event_dict); Py_DECREF(m_event_dict); diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 6b3f745b899..a54d4909db9 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -1428,15 +1428,15 @@ PyObject *initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack PyDict_SetItemString(d, "mouse", gp_PythonMouse->NewProxy(true)); PyObject* joylist = PyList_New(JOYINDEX_MAX); - SCA_JoystickManager* joyevent = (SCA_JoystickManager*)gp_KetsjiScene->GetLogicManager()->FindEventManager(SCA_EventManager::JOY_EVENTMGR); for (int i=0; iGetJoystickDevice(i); + SCA_Joystick* joy = SCA_Joystick::GetInstance(i); if (joy && joy->Connected()) { gp_PythonJoysticks[i] = new SCA_PythonJoystick(joy); PyObject* tmp = gp_PythonJoysticks[i]->NewProxy(true); Py_INCREF(tmp); PyList_SET_ITEM(joylist, i, tmp); } else { + joy->ReleaseInstance(); Py_INCREF(Py_None); PyList_SET_ITEM(joylist, i, Py_None); }