From f5e020a7a6ad6451fcaf075ae14f7014b8a4faea Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 14 Jul 2016 17:28:28 +1000 Subject: PyAPI: fix memory leaks in dictionary assignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to Kévin Dietrich for spotting driver leak, checked other uses of PyDict_SetItem and found more. --- .../gameengine/GameLogic/SCA_PythonController.cpp | 4 ++- source/gameengine/GameLogic/SCA_PythonKeyboard.cpp | 26 ++++++++++++------ source/gameengine/GameLogic/SCA_PythonMouse.cpp | 31 +++++++++++++++------- 3 files changed, 42 insertions(+), 19 deletions(-) (limited to 'source/gameengine') diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index 25936b34fde..fd2e723741c 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -163,7 +163,9 @@ void SCA_PythonController::SetNamespace(PyObject* pythondictionary) /* Without __file__ set the sys.argv[0] is used for the filename * which ends up with lines from the blender binary being printed in the console */ - PyDict_SetItemString(m_pythondictionary, "__file__", PyUnicode_From_STR_String(m_scriptName)); + PyObject *value = PyUnicode_From_STR_String(m_scriptName); + PyDict_SetItemString(m_pythondictionary, "__file__", value); + Py_DECREF(value); } #endif diff --git a/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp b/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp index 19aae46f2a3..9a96a7b0334 100644 --- a/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp +++ b/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp @@ -113,11 +113,15 @@ PyObject *SCA_PythonKeyboard::pyattr_get_events(void *self_v, const KX_PYATTRIBU { SCA_PythonKeyboard* self = static_cast(self_v); - for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++) - { + for (int i = SCA_IInputDevice::KX_BEGINKEY; i <= SCA_IInputDevice::KX_ENDKEY; i++) { const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); - - PyDict_SetItem(self->m_event_dict, PyLong_FromLong(i), PyLong_FromLong(inevent.m_status)); + PyObject *key = PyLong_FromLong(i); + PyObject *value = PyLong_FromLong(inevent.m_status); + + PyDict_SetItem(self->m_event_dict, key, value); + + Py_DECREF(key); + Py_DECREF(value); } Py_INCREF(self->m_event_dict); return self->m_event_dict; @@ -129,12 +133,18 @@ PyObject *SCA_PythonKeyboard::pyattr_get_active_events(void *self_v, const KX_PY PyDict_Clear(self->m_event_dict); - for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++) - { + for (int i = SCA_IInputDevice::KX_BEGINKEY; i <= SCA_IInputDevice::KX_ENDKEY; i++) { const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); - if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) - PyDict_SetItem(self->m_event_dict, PyLong_FromLong(i), PyLong_FromLong(inevent.m_status)); + if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) { + PyObject *key = PyLong_FromLong(i); + PyObject *value = PyLong_FromLong(inevent.m_status); + + PyDict_SetItem(self->m_event_dict, key, value); + + Py_DECREF(key); + Py_DECREF(value); + } } Py_INCREF(self->m_event_dict); return self->m_event_dict; diff --git a/source/gameengine/GameLogic/SCA_PythonMouse.cpp b/source/gameengine/GameLogic/SCA_PythonMouse.cpp index 1617f714113..184b306a665 100644 --- a/source/gameengine/GameLogic/SCA_PythonMouse.cpp +++ b/source/gameengine/GameLogic/SCA_PythonMouse.cpp @@ -96,11 +96,15 @@ PyObject *SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_ { SCA_PythonMouse* self = static_cast(self_v); - for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++) - { - const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); - - PyDict_SetItem(self->m_event_dict, PyLong_FromLong(i), PyLong_FromLong(inevent.m_status)); + for (int i = SCA_IInputDevice::KX_BEGINMOUSE; i <= SCA_IInputDevice::KX_ENDMOUSE; i++) { + const SCA_InputEvent &inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); + PyObject *key = PyLong_FromLong(i); + PyObject *value = PyLong_FromLong(inevent.m_status); + + PyDict_SetItem(self->m_event_dict, key, value); + + Py_DECREF(key); + Py_DECREF(value); } Py_INCREF(self->m_event_dict); return self->m_event_dict; @@ -112,12 +116,19 @@ PyObject *SCA_PythonMouse::pyattr_get_active_events(void *self_v, const KX_PYATT PyDict_Clear(self->m_event_dict); - for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++) - { - const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); + for (int i = SCA_IInputDevice::KX_BEGINMOUSE; i <= SCA_IInputDevice::KX_ENDMOUSE; i++) { + const SCA_InputEvent &inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i); - if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) - PyDict_SetItem(self->m_event_dict, PyLong_FromLong(i), PyLong_FromLong(inevent.m_status)); + if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) { + + PyObject *key = PyLong_FromLong(i); + PyObject *value = PyLong_FromLong(inevent.m_status); + + PyDict_SetItem(self->m_event_dict, key, value); + + Py_DECREF(key); + Py_DECREF(value); + } } Py_INCREF(self->m_event_dict); return self->m_event_dict; -- cgit v1.2.3