From 85e82de916e8799ee1198c63566b7a1f5e47f514 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 1 Jun 2020 11:54:45 +0200 Subject: [PATCH] QPainter: replace manual memory management [3/5]: engine Use unique_ptr to indicate ownership, even though it's conditional (on QPaintEngine::autoDestruct()). That just requires a custom deleter. Change-Id: Icf8e356c333f9617b2e5172b14f13197e63c9502 Reviewed-by: Qt CI Bot Reviewed-by: Allan Sandfeld Jensen --- src/gui/painting/qpainter.cpp | 12 +++++------- src/gui/painting/qpainter_p.h | 10 +++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp index eca80f55f0..eb51e9cc24 100644 --- a/src/gui/painting/qpainter.cpp +++ b/src/gui/painting/qpainter.cpp @@ -1545,7 +1545,7 @@ QPaintDevice *QPainter::device() const bool QPainter::isActive() const { Q_D(const QPainter); - return d->engine; + return d->engine != nullptr; } void QPainterPrivate::initFrom(const QPaintDevice *device) @@ -1744,7 +1744,7 @@ bool QPainter::begin(QPaintDevice *pd) else if (pd->devType() == QInternal::Image) static_cast(pd)->detach(); - d->engine = pd->paintEngine(); + d->engine.reset(pd->paintEngine()); if (!d->engine) { qWarning("QPainter::begin: Paint device returned engine == 0, type: %d", pd->devType()); @@ -1753,7 +1753,7 @@ bool QPainter::begin(QPaintDevice *pd) d->device = pd; - d->extended = d->engine->isExtended() ? static_cast(d->engine) : nullptr; + d->extended = d->engine->isExtended() ? static_cast(d->engine.get()) : nullptr; if (d->emulationEngine) d->emulationEngine->real_engine = d->extended; @@ -1911,9 +1911,7 @@ bool QPainter::end() qWarning("QPainter::end: Painter ended with %d saved states", int(d->savedStates.size())); } - if (d->engine->autoDestruct()) { - delete d->engine; - } + d->engine.reset(); if (d->emulationEngine) { delete d->emulationEngine; @@ -1939,7 +1937,7 @@ bool QPainter::end() QPaintEngine *QPainter::paintEngine() const { Q_D(const QPainter); - return d->engine; + return d->engine.get(); } /*! diff --git a/src/gui/painting/qpainter_p.h b/src/gui/painting/qpainter_p.h index 8994bcd3e7..c9caca7901 100644 --- a/src/gui/painting/qpainter_p.h +++ b/src/gui/painting/qpainter_p.h @@ -268,7 +268,15 @@ public: QPaintDevice *device; QPaintDevice *original_device; QPaintDevice *helper_device; - QPaintEngine *engine; + struct QPaintEngineDestructor { + void operator()(QPaintEngine *pe) const noexcept + { + if (pe && pe->autoDestruct()) + delete pe; + } + }; + + std::unique_ptr engine; QEmulationPaintEngine *emulationEngine; QPaintEngineEx *extended; QBrush colorBrush; // for fill with solid color