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 <axel.spoerl@qt.io>
(cherry picked from commit b50a2761e735208eeb62042ecfaba7a76e580454)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2025-04-15 12:58:40 +02:00 committed by Qt Cherry-pick Bot
parent 4f89fbc9a9
commit c9ba8760de

View File

@ -19,6 +19,15 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
struct DeleteLater
{
void operator()(QObject *o) const
{
if (o)
o->deleteLater();
}
};
/* /*
Internal class QAlphaWidget. Internal class QAlphaWidget.
@ -58,7 +67,7 @@ private:
QElapsedTimer checkTime; QElapsedTimer checkTime;
}; };
static QAlphaWidget* q_blend = nullptr; static std::unique_ptr<QAlphaWidget, DeleteLater> q_blend;
/* /*
Constructs a QAlphaWidget. Constructs a QAlphaWidget.
@ -227,8 +236,7 @@ void QAlphaWidget::render()
anim.stop(); anim.stop();
qApp->removeEventFilter(this); qApp->removeEventFilter(this);
widget->setWindowOpacity(1); widget->setWindowOpacity(1);
q_blend = 0; q_blend.reset();
deleteLater();
} else { } else {
widget->setWindowOpacity(alpha); widget->setWindowOpacity(alpha);
} }
@ -248,8 +256,7 @@ void QAlphaWidget::render()
lower(); lower();
} }
} }
q_blend = nullptr; q_blend.reset();
deleteLater();
} else { } else {
alphaBlend(); alphaBlend();
pm = QPixmap::fromImage(mixedImage); pm = QPixmap::fromImage(mixedImage);
@ -341,7 +348,7 @@ private:
QPixmap pm; QPixmap pm;
}; };
static QRollEffect* q_roll = nullptr; static std::unique_ptr<QRollEffect, DeleteLater> q_roll;
/* /*
Construct a QRollEffect widget. Construct a QRollEffect widget.
@ -513,8 +520,7 @@ void QRollEffect::scroll()
lower(); lower();
} }
} }
q_roll = nullptr; q_roll.reset();
deleteLater();
} }
} }
@ -524,10 +530,7 @@ void QRollEffect::scroll()
*/ */
void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time) void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time)
{ {
if (q_roll) { q_roll.reset();
q_roll->deleteLater();
q_roll = nullptr;
}
if (!w) if (!w)
return; return;
@ -537,7 +540,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time)
Qt::WindowFlags flags = Qt::ToolTip; Qt::WindowFlags flags = Qt::ToolTip;
// those can be popups - they would steal the focus, but are disabled // 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); q_roll->run(time);
} }
@ -546,10 +549,7 @@ void qScrollEffect(QWidget* w, QEffects::DirFlags orient, int time)
*/ */
void qFadeEffect(QWidget* w, int time) void qFadeEffect(QWidget* w, int time)
{ {
if (q_blend) { q_blend.reset();
q_blend->deleteLater();
q_blend = nullptr;
}
if (!w) if (!w)
return; return;
@ -560,7 +560,7 @@ void qFadeEffect(QWidget* w, int time)
Qt::WindowFlags flags = Qt::ToolTip; Qt::WindowFlags flags = Qt::ToolTip;
// those can be popups - they would steal the focus, but are disabled // 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); q_blend->run(time);
} }