From 3bf3d7cc3382e84c31b7d12b06b3d0f9c0bea83b Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 14 Nov 2018 16:18:40 +0100 Subject: [PATCH] Fix crash at shutdown if multiple windows are open The issue is that the window actions were explicitly made children of the ActionManager, but ActionManager is deleted earlier than the windows closed at shutdown. Change WindowList from a bunch of static data to an object with a lifetime and make it own the actions itself. Fix-up of 1b0d6e3c26c5dc54c6eae3cb21dc36a22ab1274c which started deleting the actions in the first place. Fixes: QTCREATORBUG-21221 Change-Id: I2e335887fa4b85b29bdaa2c908ec643b6abf3231 Reviewed-by: Ulf Hermann --- src/plugins/coreplugin/windowsupport.cpp | 27 ++++++++++++------------ src/plugins/coreplugin/windowsupport.h | 22 ++++++++++--------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/plugins/coreplugin/windowsupport.cpp b/src/plugins/coreplugin/windowsupport.cpp index 6ce31bc8a4b..17bab61bccf 100644 --- a/src/plugins/coreplugin/windowsupport.cpp +++ b/src/plugins/coreplugin/windowsupport.cpp @@ -44,11 +44,7 @@ namespace Core { namespace Internal { - -QMenu *WindowList::m_dockMenu = nullptr; -QList WindowList::m_windows; -QList WindowList::m_windowActions; -QList WindowList::m_windowActionIds; +Q_GLOBAL_STATIC(WindowList, m_windowList) WindowSupport::WindowSupport(QWidget *window, const Context &context) : QObject(window), @@ -80,7 +76,7 @@ WindowSupport::WindowSupport(QWidget *window, const Context &context) ActionManager::registerAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN, context); connect(m_toggleFullScreenAction, &QAction::triggered, this, &WindowSupport::toggleFullScreen); - WindowList::addWindow(window); + m_windowList->addWindow(window); connect(ICore::instance(), &ICore::coreAboutToClose, this, [this]() { m_shutdown = true; }); } @@ -95,7 +91,7 @@ WindowSupport::~WindowSupport() } ActionManager::unregisterAction(m_toggleFullScreenAction, Constants::TOGGLE_FULLSCREEN); ICore::removeContextObject(m_contextObject); - WindowList::removeWindow(m_window); + m_windowList->removeWindow(m_window); } } @@ -117,12 +113,12 @@ bool WindowSupport::eventFilter(QObject *obj, QEvent *event) } updateFullScreenAction(); } else if (event->type() == QEvent::WindowActivate) { - WindowList::setActiveWindow(m_window); + m_windowList->setActiveWindow(m_window); } else if (event->type() == QEvent::Hide) { // minimized windows are hidden, but we still want to show them - WindowList::setWindowVisible(m_window, m_window->isMinimized()); + m_windowList->setWindowVisible(m_window, m_window->isMinimized()); } else if (event->type() == QEvent::Show) { - WindowList::setWindowVisible(m_window, true); + m_windowList->setWindowVisible(m_window, true); } return false; } @@ -151,6 +147,11 @@ void WindowSupport::updateFullScreenAction() } } +WindowList::~WindowList() +{ + qDeleteAll(m_windowActions); +} + void WindowList::addWindow(QWidget *window) { #ifdef Q_OS_OSX @@ -163,16 +164,16 @@ void WindowList::addWindow(QWidget *window) m_windows.append(window); Id id = Id("QtCreator.Window.").withSuffix(m_windows.size()); m_windowActionIds.append(id); - auto action = new QAction(window->windowTitle(), ActionManager::instance()); + auto action = new QAction(window->windowTitle()); m_windowActions.append(action); - QObject::connect(action, &QAction::triggered, [action]() { WindowList::activateWindow(action); }); + QObject::connect(action, &QAction::triggered, [action, this]() { activateWindow(action); }); action->setCheckable(true); action->setChecked(false); Command *cmd = ActionManager::registerAction(action, id); cmd->setAttribute(Command::CA_UpdateText); ActionManager::actionContainer(Constants::M_WINDOW)->addAction(cmd, Constants::G_WINDOW_LIST); action->setVisible(window->isVisible() || window->isMinimized()); // minimized windows are hidden but should be shown - QObject::connect(window, &QWidget::windowTitleChanged, [window]() { WindowList::updateTitle(window); }); + QObject::connect(window, &QWidget::windowTitleChanged, [window, this]() { updateTitle(window); }); if (m_dockMenu) m_dockMenu->addAction(action); if (window->isActiveWindow()) diff --git a/src/plugins/coreplugin/windowsupport.h b/src/plugins/coreplugin/windowsupport.h index 1e9d27139a3..5531306fe0f 100644 --- a/src/plugins/coreplugin/windowsupport.h +++ b/src/plugins/coreplugin/windowsupport.h @@ -41,19 +41,21 @@ namespace Internal { class WindowList { public: - static void addWindow(QWidget *window); - static void removeWindow(QWidget *window); - static void setActiveWindow(QWidget *window); - static void setWindowVisible(QWidget *window, bool visible); + ~WindowList(); + + void addWindow(QWidget *window); + void removeWindow(QWidget *window); + void setActiveWindow(QWidget *window); + void setWindowVisible(QWidget *window, bool visible); private: - static void activateWindow(QAction *action); - static void updateTitle(QWidget *window); + void activateWindow(QAction *action); + void updateTitle(QWidget *window); - static QMenu *m_dockMenu; - static QList m_windows; - static QList m_windowActions; - static QList m_windowActionIds; + QMenu *m_dockMenu = nullptr; + QList m_windows; + QList m_windowActions; + QList m_windowActionIds; }; class WindowSupport : public QObject