From faffaaae29f0d1d4d6b1f8b1b1784cabb691769a Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Tue, 1 Sep 2015 18:45:30 +0200 Subject: [PATCH] Fix bug when a wxToolBar tool is deleted from its click handler Deleting a tool from its own event handler resulted in at least in an assert and could also lead to a crash as an already deallocated pointer was dereferenced. Fix this by checking if the tool with the same ID is still available after the event handler finishes. See #16762. --- src/msw/toolbar.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index ff76183d42..9ab3d9baf7 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -1362,6 +1362,17 @@ bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) bool allowLeftClick = OnLeftClick(id, toggled); + // Check if the tool hasn't been deleted in the event handler (notice that + // it's also possible that this tool was deleted and a new tool with the + // same ID was created, so we really need to check if the pointer to the + // tool with the given ID didn't change, not just that it's non null). + if ( FindById(id) != tool ) + { + // The rest of this event handler deals with updating the tool and must + // not be executed if the tool doesn't exist any more. + return true; + } + // Restore the unpressed state. Enabled/toggled state might have been // changed since so take care of it. if (tool->IsEnabled())