tst_QMenu: fix memleaks in tearOffMenuNotDisplayed()

QActions can be added to many widgets at the same time, so addAction()
doesn't reparent them.

Having no (QObject) parents, the QAction objects in this test were
consequently leaked.

Fix by giving them parents.

Passing `this` would have worked, but has the drawback that if you run
the test twice (by naming it twice on the command line), actions with
identical shortcuts would accumulate and potentially change the
result. Therefore, add an automatic QObject to act as their
parent. This is the minimally-invasive fix.

Amends 198225983df9f402bb368b449f1abeea95ff0dce.

Pick-to: 6.9 6.8 6.5
Change-Id: I59881b10076ba790502483242f620aeed3e99575
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Marc Mutz 2025-03-25 18:17:09 +01:00
parent 850d352111
commit ad0d6be778

View File

@ -1982,11 +1982,13 @@ void tst_QMenu::QTBUG_61039_menu_shortcuts()
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))
QSKIP("Window activation is not supported");
QAction *actionKamen = new QAction("Action Kamen");
QObject reaper;
QAction *actionKamen = new QAction("Action Kamen", &reaper);
#if QT_CONFIG(shortcut)
actionKamen->setShortcut(QKeySequence(QLatin1String("K")));
#endif
QAction *actionJoe = new QAction("Action Joe");
QAction *actionJoe = new QAction("Action Joe", &reaper);
#if QT_CONFIG(shortcut)
actionJoe->setShortcut(QKeySequence(QLatin1String("Ctrl+J")));
#endif