From 2ec89d4908374cdb2ab7c3bc7adcf153a1ea587e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 26 Mar 2025 09:41:46 +0100 Subject: [PATCH] tst_QCalendarWidget: fix memleak in showPrevNext() QObject parents own their children, but not the other way around. So the parent-less QWidget *parent created on the heap was leaked. To fix, create it on the stack instead, but for that to work, it must be declared _before_ its eventual child, otherwise the parent would delete the child as a QObject child, followed by the compiler deleting the child as an automatic variable = double-delete. Amends ab536c3c71deaaa3b67ca87c47628355d9f348e8. Pick-to: 6.8 6.5 5.15 Change-Id: Ibccf229fc056665e3b854e1a94e1f4b5bf7a89e1 Reviewed-by: Friedemann Kleint (cherry picked from commit 3c7719f2c248a2710c75fa47e12759188abf673d) Reviewed-by: Qt Cherry-pick Bot --- .../widgets/qcalendarwidget/tst_qcalendarwidget.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/auto/widgets/widgets/qcalendarwidget/tst_qcalendarwidget.cpp b/tests/auto/widgets/widgets/qcalendarwidget/tst_qcalendarwidget.cpp index e5c82f01d58..2a65f19a458 100644 --- a/tests/auto/widgets/widgets/qcalendarwidget/tst_qcalendarwidget.cpp +++ b/tests/auto/widgets/widgets/qcalendarwidget/tst_qcalendarwidget.cpp @@ -313,6 +313,7 @@ void tst_QCalendarWidget::showPrevNext() void tst_QCalendarWidget::firstDayOfWeek() { // Ensure the default locale is chosen. + QWidget parent; QCalendarWidget calendar; QLocale locale; QCOMPARE(calendar.firstDayOfWeek(), locale.firstDayOfWeek()); @@ -335,17 +336,16 @@ void tst_QCalendarWidget::firstDayOfWeek() QCOMPARE(calendar.firstDayOfWeek(), frenchLocale.firstDayOfWeek()); // Ensure that setting the locale of parent widget has an effect. - QWidget* parent = new QWidget; - calendar.setParent(parent); + calendar.setParent(&parent); QLocale hausaLocale(QLocale::Hausa); - parent->setLocale(hausaLocale); + parent.setLocale(hausaLocale); QCOMPARE(calendar.firstDayOfWeek(), hausaLocale.firstDayOfWeek()); // Ensure that widget-specific locale takes precedence over parent. calendar.setLocale(germanLocale); // Sanity check... QCOMPARE(calendar.locale(), germanLocale); - QCOMPARE(parent->locale(), hausaLocale); + QCOMPARE(parent.locale(), hausaLocale); QCOMPARE(calendar.firstDayOfWeek(), germanLocale.firstDayOfWeek()); }