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).

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>
(cherry picked from commit c9ba8760deec4b2f5c245e9120d59b622b383ceb)
This commit is contained in:
Volker Hilsheimer 2025-04-15 12:58:40 +02:00 committed by Qt Cherry-pick Bot
parent b93d9dc01a
commit e2beffe6f5

View File

@ -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<QAlphaWidget, DeleteLater> 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<QRollEffect, DeleteLater> 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);
}