From: Vadim Zeitlin Date: Fri, 23 Aug 2002 18:05:58 +0000 (+0000) Subject: fixed crash when calling Delete(root) and also when calling Delete(descendant_of_sele... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/7475e8a313860781303b11e3a13e25e1a0914861?ds=inline fixed crash when calling Delete(root) and also when calling Delete(descendant_of_selected_item) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16714 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index 4d572e4b92..2fbe7cde63 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -322,6 +322,23 @@ static void EventFlagsToSelType(long style, unselect_others = !(extended_select || (ctrlDown && is_multiple)); } +// check if the given item is under another one +static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item) +{ + while ( item ) + { + if ( item == parent ) + { + // item is a descendant of parent + return TRUE; + } + + item = item->GetParent(); + } + + return FALSE; +} + // ----------------------------------------------------------------------------- // wxTreeRenameTimer (internal) // ----------------------------------------------------------------------------- @@ -1416,33 +1433,31 @@ void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId) wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; - // 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 ) + wxGenericTreeItem *parent = item->GetParent(); + + // don't keep stale pointers around! + if ( IsDescendantOf(item, m_key_current) ) { - if ( itemKey == item ) - { - // m_key_current is a descendant of the item being deleted - changeKeyCurrent = TRUE; - break; - } - itemKey = itemKey->GetParent(); + m_key_current = parent; } - wxGenericTreeItem *parent = item->GetParent(); + if ( IsDescendantOf(item, m_current) ) + { + m_current = parent; + } + + // remove the item from the tree if ( parent ) { parent->GetChildren().Remove( item ); // remove by value } - - if ( changeKeyCurrent ) + else // deleting the root { - // may be NULL or not - m_key_current = parent; + // nothing will be left in the tree + m_anchor = NULL; } + // and delete all of its children and the item itself now item->DeleteChildren(this); SendDeleteEvent(item); delete item; @@ -1452,12 +1467,7 @@ void wxGenericTreeCtrl::DeleteAllItems() { if ( m_anchor ) { - m_dirty = TRUE; - - m_anchor->DeleteChildren(this); - delete m_anchor; - - m_anchor = NULL; + Delete(m_anchor); } }