From 96efff81c7cf07c9c96968b6c282c872433c3f81 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 1 Oct 2023 08:48:08 +0200 Subject: [PATCH] Sketcher: Fix segfault when activating a tool in a different view ================================================================= When in Sketcher edit mode, a tool button is activated, while the view has been changing to view of a different type, it segfaults. This commit checks the pointer of the view to ensure correct type before activation, and refusing to activate if not of the correct type. fixes #10809 --- src/Mod/Sketcher/Gui/DrawSketchHandler.cpp | 18 ++++++++++++------ src/Mod/Sketcher/Gui/ViewProviderSketch.cpp | 11 +++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp b/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp index 12adc95424ab..1159308ecb36 100644 --- a/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp +++ b/src/Mod/Sketcher/Gui/DrawSketchHandler.cpp @@ -283,14 +283,20 @@ void DrawSketchHandler::activate(ViewProviderSketch* vp) sketchgui = vp; // save the cursor at the time the DSH is activated - Gui::MDIView* view = Gui::getMainWindow()->activeWindow(); - Gui::View3DInventorViewer* viewer = static_cast(view)->getViewer(); - oldCursor = viewer->getWidget()->cursor(); + auto* view = dynamic_cast(Gui::getMainWindow()->activeWindow()); + + if (view) { + Gui::View3DInventorViewer* viewer = dynamic_cast(view)->getViewer(); + oldCursor = viewer->getWidget()->cursor(); - updateCursor(); + updateCursor(); - this->preActivated(); - this->activated(); + this->preActivated(); + this->activated(); + } + else { + sketchgui->purgeHandler(); + } } void DrawSketchHandler::deactivate() diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index 120e1e5afaef..8fd0d9c5d76e 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -565,10 +565,13 @@ void ViewProviderSketch::purgeHandler() Gui::Selection().clearSelection(); // ensure that we are in sketch only selection mode - Gui::MDIView* mdi = Gui::Application::Instance->editDocument()->getActiveView(); - Gui::View3DInventorViewer* viewer; - viewer = static_cast(mdi)->getViewer(); - viewer->setSelectionEnabled(false); + auto* view = dynamic_cast(Gui::Application::Instance->editDocument()->getActiveView()); + + if(view) { + Gui::View3DInventorViewer* viewer; + viewer = static_cast(view)->getViewer(); + viewer->setSelectionEnabled(false); + } } void ViewProviderSketch::setAxisPickStyle(bool on)