X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8ebec7dcd8b0b7294dbc86340d52a10b45bc80db..d863389f773348f5b6e06243050d4379d66de05d:/src/common/event.cpp diff --git a/src/common/event.cpp b/src/common/event.cpp index dc121dee42..9e8fbdf508 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -45,6 +45,13 @@ #include "wx/thread.h" +#if wxUSE_BASE + #include "wx/ptr_scpd.h" + + wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr) + wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr) +#endif // wxUSE_BASE + // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -176,6 +183,7 @@ DEFINE_EVENT_TYPE(wxEVT_COMMAND_COMBOBOX_SELECTED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_RCLICKED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_ENTER) DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRL_UPDATED) +DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED) // Mouse event types @@ -406,13 +414,15 @@ wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId) wxString wxCommandEvent::GetString() const { - if(m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject) + if (m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject) + { return m_cmdString; + } else { #if wxUSE_TEXTCTRL wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl); - if(txt) + if ( txt ) return txt->GetValue(); else #endif // wxUSE_TEXTCTRL @@ -1012,7 +1022,6 @@ void wxEventHashTable::GrowEventTypeTable() delete[] oldEventTypeTable; } - // ---------------------------------------------------------------------------- // wxEvtHandler // ---------------------------------------------------------------------------- @@ -1023,6 +1032,8 @@ void wxEventHashTable::GrowEventTypeTable() wxEvtHandler::wxEvtHandler() { + m_beingDeleted = false; + m_nextHandler = (wxEvtHandler *) NULL; m_previousHandler = (wxEvtHandler *) NULL; m_enabled = true; @@ -1036,6 +1047,8 @@ wxEvtHandler::wxEvtHandler() wxEvtHandler::~wxEvtHandler() { + m_beingDeleted = true; + // Takes itself out of the list of handlers if (m_previousHandler) m_previousHandler->m_nextHandler = m_nextHandler; @@ -1052,6 +1065,19 @@ wxEvtHandler::~wxEvtHandler() { wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it; + // Remove ourselves from sink destructor notifications + wxEvtHandler *eventSink = entry->m_eventSink; + if ( eventSink ) + { + wxEventConnectionRef * const + evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + { + eventSink->RemoveNode(evtConnRef); + delete evtConnRef; + } + } + if (entry->m_callbackUserData) delete entry->m_callbackUserData; delete entry; @@ -1060,14 +1086,25 @@ wxEvtHandler::~wxEvtHandler() }; if (m_pendingEvents) + { + // At this time, we could still be used from other threads. + // Continue to use sync objects. + wxENTER_CRIT_SECT( m_pendingEventsLock ); + m_pendingEvents->DeleteContents(true); - delete m_pendingEvents; + delete m_pendingEvents; + m_pendingEvents = NULL; + + wxLEAVE_CRIT_SECT( m_pendingEventsLock ); + } // Remove us from wxPendingEvents if necessary. if ( wxPendingEvents ) { - if(wxPendingEventsLocker) +#if wxUSE_THREADS + if (wxPendingEventsLocker) wxENTER_CRIT_SECT(*wxPendingEventsLocker); +#endif if ( wxPendingEvents->DeleteObject(this) ) { @@ -1077,8 +1114,10 @@ wxEvtHandler::~wxEvtHandler() } //else: we weren't in this list at all, it's ok - if(wxPendingEventsLocker) +#if wxUSE_THREADS + if (wxPendingEventsLocker) wxLEAVE_CRIT_SECT(*wxPendingEventsLocker); +#endif } // we only delete object data, not untyped @@ -1101,23 +1140,20 @@ bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event) #endif // wxUSE_THREADS -void wxEvtHandler::AddPendingEvent(const wxEvent& event) +void wxEvtHandler::QueueEvent(wxEvent *event) { - // 1) Add event to list of pending events of this event handler + wxCHECK_RET( event, "NULL event can't be posted" ); - wxEvent *eventCopy = event.Clone(); - - // we must be able to copy the events here so the event class must - // implement Clone() properly instead of just providing a NULL stab for it - wxCHECK_RET( eventCopy, - _T("events of this type aren't supposed to be posted") ); + // Catch the situation where destructor is already invoked (in another thread) + if( m_beingDeleted ) return; + // 1) Add this event to our list of pending events wxENTER_CRIT_SECT( m_pendingEventsLock ); if ( !m_pendingEvents ) m_pendingEvents = new wxList; - m_pendingEvents->Append(eventCopy); + m_pendingEvents->Append(event); wxLEAVE_CRIT_SECT( m_pendingEventsLock ); @@ -1142,13 +1178,24 @@ void wxEvtHandler::ProcessPendingEvents() { wxENTER_CRIT_SECT( m_pendingEventsLock ); - // this method is only called by wxApp if this handler does have - // pending events - wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(), - "should have pending events if called" ); + // This method is only called by wxApp if this handler does have + // pending events, but it happens occasionally when using multi- + // threading and we don't want a crash due to that. + if( !m_pendingEvents ) + { + wxLEAVE_CRIT_SECT( m_pendingEventsLock ); + return; + } + + if( m_pendingEvents->IsEmpty() ) + { + wxPendingEvents->DeleteObject(this); + wxLEAVE_CRIT_SECT( m_pendingEventsLock ); + return; + } wxList::compatibility_iterator node = m_pendingEvents->GetFirst(); - wxEvent * const event = wx_static_cast(wxEvent *, node->GetData()); + wxEventPtr event(wx_static_cast(wxEvent *, node->GetData())); // it's important we remove event from list before processing it, else a // nested event loop, for example from a modal dialog, might process the @@ -1158,7 +1205,17 @@ void wxEvtHandler::ProcessPendingEvents() // if there are no more pending events left, we don't need to stay in this // list if ( m_pendingEvents->IsEmpty() ) + { +#if wxUSE_THREADS + if (wxPendingEventsLocker) + wxENTER_CRIT_SECT(*wxPendingEventsLocker); +#endif wxPendingEvents->DeleteObject(this); +#if wxUSE_THREADS + if (wxPendingEventsLocker) + wxLEAVE_CRIT_SECT(*wxPendingEventsLocker); +#endif + } wxLEAVE_CRIT_SECT( m_pendingEventsLock ); @@ -1167,8 +1224,6 @@ void wxEvtHandler::ProcessPendingEvents() // careful: this object could have been deleted by the event handler // executed by the above ProcessEvent() call, so we can't access any fields // of this object any more - - delete event; } /* @@ -1347,6 +1402,16 @@ void wxEvtHandler::Connect( int id, int lastId, // Insert at the front of the list so most recent additions are found first m_dynamicEvents->Insert( (wxObject*) entry ); + + // Make sure we get to know when a sink is destroyed + if ( eventSink && eventSink != this ) + { + wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + evtConnRef->IncRef( ); + else + evtConnRef = new wxEventConnectionRef(this, eventSink); + } } bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType, @@ -1357,6 +1422,14 @@ bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType, if (!m_dynamicEvents) return false; + // Remove connection from tracker node (wxEventConnectionRef) + if ( eventSink && eventSink != this ) + { + wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + evtConnRef->DecRef(); + } + wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(); while (node) { @@ -1447,6 +1520,46 @@ void *wxEvtHandler::DoGetClientData() const return m_clientData; } +// A helper to find an wxEventConnectionRef object +wxEventConnectionRef * +wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink) +{ + for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt ) + { + // we only want wxEventConnectionRef nodes here + wxEventConnectionRef *evtConnRef = node->ToEventConnection(); + if ( evtConnRef && evtConnRef->m_src == this ) + { + wxASSERT( evtConnRef->m_sink==eventSink ); + return evtConnRef; + } + } + + return NULL; +} + +void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink ) +{ + wxASSERT(m_dynamicEvents); + + // remove all connections with this sink + wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt; + while (node) + { + wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData(); + node_nxt = node->GetNext(); + + if ( entry->m_eventSink==sink ) + { + if (entry->m_callbackUserData) + delete entry->m_callbackUserData; + m_dynamicEvents->Erase( node ); + delete entry; + } + node = node_nxt; + } +} + #endif // wxUSE_BASE #if wxUSE_GUI