From c9ba8760deec4b2f5c245e9120d59b622b383ceb Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 15 Apr 2025 12:58:40 +0200 Subject: [PATCH] Widget effects DRY: replace manual pointer handling with unique_ptr Instead of repeatedly resetting of the static pointers after (or before) calling deleteLater(), use a unique_ptr with a custom deleter. Since we use deleteLater(), nothing will happen if one of those widgets does leak to the point where static objects are destroyed; we'd at most call deleteLater() at this point, which won't do anything (if we enqueue a DeferredDelete event at all, then it will never be processed). Pick-to: 6.8 Task-number: QTBUG-135976 Change-Id: I36a4780093eafd064dcb1a72696c1d9b21483b77 Reviewed-by: Axel Spoerl (cherry picked from commit b50a2761e735208eeb62042ecfaba7a76e580454) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qeffects.cpp | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/widgets/widgets/qeffects.cpp b/src/widgets/widgets/qeffects.cpp index 74ac24a2b0a..50c09ec4daf 100644 --- a/src/widgets/widgets/qeffects.cpp +++ b/src/widgets/widgets/qeffects.cpp @@ -19,6 +19,15 @@ QT_BEGIN_NAMESPACE +struct DeleteLater +{ + void operator()(QObject *o) const + { + if (o) + o->deleteLater(); + } +}; + /* Internal class QAlphaWidget. @@ -58,7 +67,7 @@ private: QElapsedTimer checkTime; }; -static QAlphaWidget* q_blend = nullptr; +static std::unique_ptr q_blend; /* Constructs a QAlphaWidget. @@ -227,8 +236,7 @@ void QAlphaWidget::render() anim.stop(); qApp->removeEventFilter(this); widget->setWindowOpacity(1); - q_blend = 0; - deleteLater(); + q_blend.reset(); } else { widget->setWindowOpacity(alpha); } @@ -248,8 +256,7 @@ void QAlphaWidget::render() lower(); } } - q_blend = nullptr; - deleteLater(); + q_blend.reset(); } else { alphaBlend(); pm = QPixmap::fromImage(mixedImage); @@ -341,7 +348,7 @@ private: QPixmap pm; }; -static QRollEffect* q_roll = nullptr; +static std::unique_ptr q_roll; /* Construct a QRollEffect widget. @@ -513,8 +520,7 @@ void QRollEffect::scroll() lower(); } } - q_roll = nullptr; - deleteLater(); + q_roll.reset(); } } @@ -524,10 +530,7 @@ void QRollEffect::scroll() */ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) { - if (q_roll) { - q_roll->deleteLater(); - q_roll = nullptr; - } + q_roll.reset(); if (!w) return; @@ -537,7 +540,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) Qt::WindowFlags flags = Qt::ToolTip; // those can be popups - they would steal the focus, but are disabled - q_roll = new QRollEffect(w, flags, orient); + q_roll.reset(new QRollEffect(w, flags, orient)); q_roll->run(time); } @@ -546,10 +549,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) */ void qFadeEffect(QWidget* w, int time) { - if (q_blend) { - q_blend->deleteLater(); - q_blend = nullptr; - } + q_blend.reset(); if (!w) return; @@ -560,7 +560,7 @@ void qFadeEffect(QWidget* w, int time) Qt::WindowFlags flags = Qt::ToolTip; // those can be popups - they would steal the focus, but are disabled - q_blend = new QAlphaWidget(w, flags); + q_blend.reset(new QAlphaWidget(w, flags)); q_blend->run(time); }