diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index 86c103323724..ef5e06a8aad0 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -253,8 +253,20 @@ QgsMapCanvas::~QgsMapCanvas() if ( mMapTool ) { mMapTool->deactivate(); + disconnect( mMapTool, &QObject::destroyed, this, &QgsMapCanvas::mapToolDestroyed ); mMapTool = nullptr; } + + // we also clear the canvas pointer for all child map tools. We're now in a partially destroyed state and it's + // no longer safe for map tools to try to cleanup things in the canvas during their destruction (such as removing + // associated canvas items) + // NOTE -- it may be better to just delete the map tool children here upfront? + const QList< QgsMapTool * > tools = findChildren< QgsMapTool *>(); + for ( QgsMapTool *tool : tools ) + { + tool->mCanvas = nullptr; + } + mLastNonZoomMapTool = nullptr; cancelJobs(); diff --git a/src/gui/qgsmaptool.h b/src/gui/qgsmaptool.h index 4bb2ada187c6..e9fc817795fc 100644 --- a/src/gui/qgsmaptool.h +++ b/src/gui/qgsmaptool.h @@ -353,6 +353,7 @@ class GUI_EXPORT QgsMapTool : public QObject //! The translated name of the map tool QString mToolName; + friend class QgsMapCanvas; friend class TestQgsMapToolEdit; };