Skip to content

Commit

Permalink
App: fix crash at shutdown in some conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Dec 15, 2023
1 parent 08997ae commit c8dabc2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/app/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ AppContext::AppContext(MainWindow* wnd)
QObject::connect(
m_wnd->widgetPageDocuments(), &WidgetMainControl::currentDocumentIndexChanged,
this, &AppContext::onCurrentDocumentIndexChanged
);
);
}

GuiApplication* AppContext::guiApp() const
Expand All @@ -41,7 +41,8 @@ TaskManager* AppContext::taskMgr() const

QWidget* AppContext::pageDocuments_widgetLeftSideBar() const
{
return m_wnd->widgetPageDocuments()->widgetLeftSideBar();
const WidgetMainControl* pageDocs = m_wnd->widgetPageDocuments();
return pageDocs ? pageDocs->widgetLeftSideBar() : nullptr;
}

QWidget* AppContext::widgetMain() const
Expand Down
9 changes: 9 additions & 0 deletions src/app/commands_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ QAction* CommandContainer::findCommandAction(std::string_view name) const
return cmd ? cmd->action() : nullptr;
}

void CommandContainer::clear()
{
for (auto [name, cmd] : m_mapCommand) {
delete cmd;
}

m_mapCommand.clear();
}

void CommandContainer::addCommand_impl(std::string_view name, Command* cmd)
{
assert(m_appContext != nullptr);
Expand Down
2 changes: 2 additions & 0 deletions src/app/commands_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class CommandContainer {
// The command name is implicit and found by assuming the presence of CmdType::Name class member
template<typename CmdType, typename... Args> CmdType* addNamedCommand(Args... p);

void clear();

private:
void addCommand_impl(std::string_view name, Command* cmd);

Expand Down
1 change: 1 addition & 0 deletions src/app/commands_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CommandMainWidgetToggleFullscreen : public Command {
class CommandLeftSidebarWidgetToggle : public Command {
public:
CommandLeftSidebarWidgetToggle(IAppContext* context);

void execute() override;
bool getEnabledStatus() const override;

Expand Down
16 changes: 10 additions & 6 deletions src/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ MainWindow::MainWindow(GuiApplication* guiApp, QWidget* parent)

MainWindow::~MainWindow()
{
// Force deletion of Command objects as some of them are event filters of MainWindow widgets
m_cmdContainer.clear();
delete m_ui;
}

Expand Down Expand Up @@ -284,18 +286,20 @@ void MainWindow::updateCurrentPage()
m_appContext->setCurrentPage(newPage);
}

IWidgetMainPage* MainWindow::widgetMainPage(IAppContext::Page page) const
{
auto it = m_mapWidgetPage.find(page);
return it != m_mapWidgetPage.cend() ? it->second : nullptr;
}

WidgetMainHome* MainWindow::widgetPageHome() const
{
auto it = m_mapWidgetPage.find(IAppContext::Page::Home);
assert(it != m_mapWidgetPage.cend());
return dynamic_cast<WidgetMainHome*>(it->second);
return dynamic_cast<WidgetMainHome*>(this->widgetMainPage(IAppContext::Page::Home));
}

WidgetMainControl* MainWindow::widgetPageDocuments() const
{
auto it = m_mapWidgetPage.find(IAppContext::Page::Documents);
assert(it != m_mapWidgetPage.cend());
return dynamic_cast<WidgetMainControl*>(it->second);
return dynamic_cast<WidgetMainControl*>(this->widgetMainPage(IAppContext::Page::Documents));
}

} // namespace Mayo
1 change: 1 addition & 0 deletions src/app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MainWindow : public QMainWindow {
void updateControlsActivation();
void updateCurrentPage();

IWidgetMainPage* widgetMainPage(IAppContext::Page page) const;
WidgetMainHome* widgetPageHome() const;
WidgetMainControl* widgetPageDocuments() const;

Expand Down

0 comments on commit c8dabc2

Please sign in to comment.