Skip to content

Commit

Permalink
Address a memory leak when updating menus (Fixes issue #238)
Browse files Browse the repository at this point in the history
I'd like to come up with a more smart pointer based fix, but this
looks correct to me in my tests. We can always refactor it in the future.
  • Loading branch information
eteran committed Jan 3, 2021
1 parent 355be64 commit 147ae1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1439,10 +1439,12 @@ void MainWindow::updateUserMenus(DocumentWidget *document) {
ui.menu_Shell->addSeparator();
ui.menu_Shell->addActions(shellMenu->actions());

auto shellGroup = new QActionGroup(this);
shellGroup->setExclusive(false);
addToGroup(shellGroup, shellMenu);
connect(shellGroup, &QActionGroup::triggered, this, &MainWindow::shellTriggered);
delete shellGroup_;
shellGroup_ = new QActionGroup(this);
shellGroup_->setExclusive(false);
addToGroup(shellGroup_, shellMenu);
connect(shellGroup_, &QActionGroup::triggered, this, &MainWindow::shellTriggered);
delete shellMenu;

auto macroMenu = createUserMenu(document, MacroMenuData, CommandTypes::Macro);
ui.menu_Macro->clear();
Expand All @@ -1454,12 +1456,15 @@ void MainWindow::updateUserMenus(DocumentWidget *document) {
ui.menu_Macro->addSeparator();
ui.menu_Macro->addActions(macroMenu->actions());

auto macroGroup = new QActionGroup(this);
macroGroup->setExclusive(false);
addToGroup(macroGroup, macroMenu);
connect(macroGroup, &QActionGroup::triggered, this, &MainWindow::macroTriggered);
delete macroGroup_;
macroGroup_ = new QActionGroup(this);
macroGroup_->setExclusive(false);
addToGroup(macroGroup_, macroMenu);
connect(macroGroup_, &QActionGroup::triggered, this, &MainWindow::macroTriggered);
delete macroMenu;

// update background menu, which is owned by a single document
delete document->contextMenu_;
document->contextMenu_ = createUserMenu(document, BGMenuData, CommandTypes::Context);

// handler for BG menu scripts
Expand Down
2 changes: 2 additions & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ public Q_SLOTS:
int iSearchHistIndex_ = 0; // find and replace dialogs
TextCursor iSearchLastBeginPos_ = {}; // beg. pos. last match of current i.s.
TextCursor iSearchStartPos_ = TextCursor(-1); // start pos. of current incr. search
QActionGroup *shellGroup_ = nullptr;
QActionGroup *macroGroup_ = nullptr;

public:
Ui::MainWindow ui;
Expand Down

0 comments on commit 147ae1f

Please sign in to comment.