diff --git a/src/ViewGuard.cpp b/src/ViewGuard.cpp index 400a0578..84f4db22 100644 --- a/src/ViewGuard.cpp +++ b/src/ViewGuard.cpp @@ -15,13 +15,8 @@ using namespace KDDockWidgets; ViewGuard::ViewGuard(View *view) - : v(view && view->inDtor() ? nullptr : view) { - if (v) { - m_onDestroy = v->beingDestroyed.connect([this] { - v = nullptr; - }); - } + setView(view); } ViewGuard::operator bool() const @@ -53,15 +48,27 @@ View *ViewGuard::view() const ViewGuard &ViewGuard::operator=(View *view) { - if (view == v) - return *this; + setView(view); + return *this; +} + +void ViewGuard::setView(View *view) +{ + if (view == v) + return; + + if (view && view->inDtor()) { + // We don't care about views that are already being in DTOR. They count as already deleted for what's ViewGuard concerned. + // This is rare anway, would need to require some reentrancy. + view = nullptr; + } - // Remove the previous connection clear(); v = view; - m_onDestroy = v->beingDestroyed.connect([this] { - v = nullptr; - }); - return *this; + if (v) { + m_onDestroy = v->beingDestroyed.connect([this] { + v = nullptr; + }); + } } diff --git a/src/ViewGuard.h b/src/ViewGuard.h index 16803008..2ce0213f 100644 --- a/src/ViewGuard.h +++ b/src/ViewGuard.h @@ -35,6 +35,7 @@ public: ViewGuard &operator=(View *); private: + void setView(View *); View *v = nullptr; KDBindings::ConnectionHandle m_onDestroy; };