From: Vadim Zeitlin Date: Tue, 14 Mar 2000 16:51:43 +0000 (+0000) Subject: fixed crash in OnChar() after m_key_current was deleted X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/aaa2f29761547b6ec2d4600a93ca4f266aae309f fixed crash in OnChar() after m_key_current was deleted git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6698 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/generic/treectrl.cpp b/src/generic/treectrl.cpp index 9a43e04af0..b4bf898049 100644 --- a/src/generic/treectrl.cpp +++ b/src/generic/treectrl.cpp @@ -1072,13 +1072,36 @@ void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId) void wxTreeCtrl::Delete(const wxTreeItemId& itemId) { wxGenericTreeItem *item = itemId.m_pItem; - wxGenericTreeItem *parent = item->GetParent(); + // don't stay with invalid m_key_current or we will crash in the next call + // to OnChar() + bool changeKeyCurrent = FALSE; + wxGenericTreeItem *itemKey = m_key_current; + while ( itemKey && !changeKeyCurrent ) + { + if ( itemKey == item ) + { + // m_key_current is a descendant of the item being deleted + changeKeyCurrent = TRUE; + } + else + { + itemKey = itemKey->GetParent(); + } + } + + wxGenericTreeItem *parent = item->GetParent(); if ( parent ) { parent->GetChildren().Remove( item ); // remove by value } + if ( changeKeyCurrent ) + { + // may be NULL or not + m_key_current = parent; + } + item->DeleteChildren(this); SendDeleteEvent(item); delete item; @@ -1331,7 +1354,12 @@ void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId, // shift press if (extended_select) { - if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem; + if ( !m_current ) + { + m_current = + m_key_current = GetRootItem().m_pItem; + } + // don't change the mark (m_current) SelectItemRange(m_current, item); } @@ -1951,7 +1979,6 @@ void wxTreeCtrl::OnChar( wxKeyEvent &event ) else { wxTreeItemId next = GetNextSibling( m_key_current ); -// if (next == 0) if (!next) { wxTreeItemId current = m_key_current; @@ -1961,7 +1988,6 @@ void wxTreeCtrl::OnChar( wxKeyEvent &event ) if (current) next = GetNextSibling( current ); } } -// if (next != 0) if (next) { SelectItem( next, unselect_others, extended_select );