[CardMenu] Free the card menus and submenus rebuilt on each right-click (#7093)

* Parent card menu submenus to the menu that owns them

QMenu::addMenu(QMenu *) does not take ownership of the submenu, and these
submenus were created without a parent. Since CardMenu is rebuilt from
scratch on every right-click, each rebuild leaked the MoveMenu, PtMenu,
card counters and related-cards submenus.

Pass the owning CardMenu as the QWidget parent so Qt's parent-child
mechanism frees them with it.

* Delete the replaced card menu in TabGame to fix menu rebuild leak

PlayerMenu creates a new CardMenu on every right-click and TabGame installs
it with QAction::setMenu(), which does not take ownership, so the previous
menu was never freed. TabGame now deletes the menu it replaces and reuses a
single empty placeholder menu instead of allocating one per update. The
installed menu is also released when the tab is destroyed.
This commit is contained in:
DawnFire42 2026-11-06 14:51:00 -05:00 • committed by GitHub
parent daa8671b4d
commit 17f583013b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 32 additions and 18 deletions

View file

@ -330,6 +330,9 @@ void TabGame::emitUserEvent()
TabGame::~TabGame()
{
if (aCardMenu && aCardMenu->menu() && aCardMenu->menu() != emptyCardMenu) {
delete aCardMenu->menu();
}
for (auto &player : game->getPlayerManager()->getPlayers()) {
player->clear();
}
@ -1158,10 +1161,16 @@ QString TabGame::getTabText() const
/**
* @param menu The menu to set. Pass in nullptr to set the menu to empty.
*
* The previous card menu is deleted here: QAction::setMenu() does not take ownership, so whoever installs the menu
* (this widget) has to dispose of the one it replaces. The empty placeholder menu is reused instead of deleted.
*/
void TabGame::setCardMenu(CardMenu *menu)
{
if (!aCardMenu) {
if (!aCardMenu || !emptyCardMenu) {
if (menu) {
menu->deleteLater();
}
return;
}
@ -1171,10 +1180,11 @@ void TabGame::setCardMenu(CardMenu *menu)
gameView->viewport()->update();
}
}
if (menu) {
aCardMenu->setMenu(menu);
} else {
aCardMenu->setMenu(new QMenu);
QMenu *previousMenu = aCardMenu->menu();
aCardMenu->setMenu(menu ? menu : emptyCardMenu);
if (previousMenu && previousMenu != emptyCardMenu && previousMenu != menu) {
previousMenu->deleteLater();
}
}
@ -1258,7 +1268,8 @@ void TabGame::createMenuItems()
gameMenu->addSeparator();
aCardMenu = gameMenu->addMenu(new QMenu(this));
emptyCardMenu = new QMenu(this);
aCardMenu = gameMenu->addMenu(emptyCardMenu);
// Sync the new action with the same state the dock button already shows.
updateInviteButtonState();
@ -1290,6 +1301,7 @@ void TabGame::createReplayMenuItems()
gameMenu->addAction(aLeaveGame);
aCardMenu = nullptr;
emptyCardMenu = nullptr;
addTabMenu(gameMenu);
}

View file

@ -101,6 +101,8 @@ private:
QAction *aInviteToGame = nullptr;
QList<QAction *> phaseActions;
QAction *aCardMenu;
/** @brief Placeholder submenu shown when no card is selected; reused instead of reallocated. */
QMenu *emptyCardMenu = nullptr;
/**
* @brief The actions associated with managing a QDockWidget