diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index 98e8f50d72..0d3571b22d 100644 --- a/wxPython/src/helpers.cpp +++ b/wxPython/src/helpers.cpp @@ -46,6 +46,7 @@ void WXDLLEXPORT wxEntryCleanup(); wxPyApp* wxPythonApp = NULL; // Global instance of application object bool wxPyDoCleanup = FALSE; +bool wxPyDoingCleanup = FALSE; #ifdef WXP_WITH_THREAD @@ -379,6 +380,7 @@ PyObject* __wxStart(PyObject* /* self */, PyObject* args) void __wxCleanup() { + wxPyDoingCleanup = TRUE; if (wxPyDoCleanup) wxEntryCleanup(); #ifdef WXP_WITH_THREAD @@ -442,15 +444,20 @@ PyObject* __wxSetDictionary(PyObject* /* self */, PyObject* args) //--------------------------------------------------------------------------- void wxPyClientData_dtor(wxPyClientData* self) { - wxPyBeginBlockThreads(); - Py_DECREF(self->m_obj); - wxPyEndBlockThreads(); + if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python + // may have already garbage collected the object... + wxPyBeginBlockThreads(); + Py_DECREF(self->m_obj); + wxPyEndBlockThreads(); + } } void wxPyUserData_dtor(wxPyUserData* self) { - wxPyBeginBlockThreads(); - Py_DECREF(self->m_obj); - wxPyEndBlockThreads(); + if (! wxPyDoingCleanup) { + wxPyBeginBlockThreads(); + Py_DECREF(self->m_obj); + wxPyEndBlockThreads(); + } } @@ -473,6 +480,8 @@ void wxPyOORClientData_dtor(wxPyOORClientData* self) { Py_INCREF(deadObjectClass); } + // TODO: If wxPyDOingCleanup, should we skip the code below? + // Clear the instance's dictionary, put the name of the old class into the // instance, and then reset the class to be the dead class. if (self->m_obj->ob_refcnt > 1) { // but only if there is more than one reference