diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2021-10-28 16:06:08 +0200 |
|---|---|---|
| committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-09-29 13:22:39 +0200 |
| commit | b65145764eeef55906503f3a958afdb8142b0006 (patch) | |
| tree | 2ac57b848c56c097def9b4b4eb8146f706f4f094 | |
| parent | 43734dd06b27e49d16de47e14d0fa4ac8de05721 (diff) | |
QQuickItemView: avoid leaking of highlights and animators
It's unclear where exactly they leak, but the tst_snippets test in
quickcontrols exposes it. Turning them into unique_ptrs is the clean
solution anyway. They are clearly owned.
Change-Id: I076ea86639b1e0ab3f688eb982b4cee6cb908f6b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 3193b20570408621d0cf9fa1665397f443d2d4d8)
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
| -rw-r--r-- | src/quick/items/qquickgridview.cpp | 22 | ||||
| -rw-r--r-- | src/quick/items/qquickitemview.cpp | 2 | ||||
| -rw-r--r-- | src/quick/items/qquickitemview_p_p.h | 2 | ||||
| -rw-r--r-- | src/quick/items/qquicklistview.cpp | 41 |
4 files changed, 34 insertions, 33 deletions
diff --git a/src/quick/items/qquickgridview.cpp b/src/quick/items/qquickgridview.cpp index 99cfec73dd..de5c9e8c83 100644 --- a/src/quick/items/qquickgridview.cpp +++ b/src/quick/items/qquickgridview.cpp @@ -400,7 +400,7 @@ int QQuickGridViewPrivate::snapIndex() const if (item->index == -1) continue; qreal itemTop = item->position(); - FxGridItemSG *hItem = static_cast<FxGridItemSG*>(highlight); + FxGridItemSG *hItem = static_cast<FxGridItemSG*>(highlight.get()); if (itemTop >= hItem->rowPos()-rowSize()/2 && itemTop < hItem->rowPos()+rowSize()/2) { FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(item); index = gridItem->index; @@ -700,10 +700,9 @@ void QQuickGridViewPrivate::createHighlight(bool onDestruction) { bool changed = false; if (highlight) { - if (trackedItem == highlight) + if (trackedItem == highlight.get()) trackedItem = nullptr; - delete highlight; - highlight = nullptr; + highlight.reset(); delete highlightXAnimator; delete highlightYAnimator; @@ -720,7 +719,8 @@ void QQuickGridViewPrivate::createHighlight(bool onDestruction) if (currentItem) { QQuickItem *item = createHighlightItem(); if (item) { - FxGridItemSG *newHighlight = new FxGridItemSG(item, q, true); + std::unique_ptr<FxGridItemSG> newHighlight + = std::make_unique<FxGridItemSG>(item, q, true); newHighlight->trackGeometry(true); if (autoHighlight) resetHighlightPosition(); @@ -731,7 +731,7 @@ void QQuickGridViewPrivate::createHighlight(bool onDestruction) highlightYAnimator->target = QQmlProperty(item, QLatin1String("y")); highlightYAnimator->userDuration = highlightMoveDuration; - highlight = newHighlight; + highlight = std::move(newHighlight); changed = true; } } @@ -762,7 +762,7 @@ void QQuickGridViewPrivate::resetHighlightPosition() { if (highlight && currentItem) { FxGridItemSG *cItem = static_cast<FxGridItemSG*>(currentItem); - static_cast<FxGridItemSG*>(highlight)->setPosition(cItem->colPos(), cItem->rowPos()); + static_cast<FxGridItemSG *>(highlight.get())->setPosition(cItem->colPos(), cItem->rowPos()); } } @@ -2101,7 +2101,8 @@ void QQuickGridView::viewportMoved(Qt::Orientations orient) if (pos != d->highlight->position()) { d->highlightXAnimator->stop(); d->highlightYAnimator->stop(); - static_cast<FxGridItemSG*>(d->highlight)->setPosition(static_cast<FxGridItemSG*>(d->highlight)->colPos(), pos); + FxGridItemSG *sgHighlight = static_cast<FxGridItemSG *>(d->highlight.get()); + sgHighlight->setPosition(sgHighlight->colPos(), pos); } else { d->updateHighlight(); } @@ -2110,7 +2111,10 @@ void QQuickGridView::viewportMoved(Qt::Orientations orient) int idx = d->snapIndex(); if (idx >= 0 && idx != d->currentIndex) { d->updateCurrent(idx); - if (d->currentItem && static_cast<FxGridItemSG*>(d->currentItem)->colPos() != static_cast<FxGridItemSG*>(d->highlight)->colPos() && d->autoHighlight) { + if (d->currentItem + && static_cast<FxGridItemSG*>(d->currentItem)->colPos() + != static_cast<FxGridItemSG*>(d->highlight.get())->colPos() + && d->autoHighlight) { if (d->flow == FlowLeftToRight) d->highlightXAnimator->to = d->currentItem->itemX(); else diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index 7146b8d1b7..f1aca5814b 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -2513,7 +2513,7 @@ void QQuickItemViewPrivate::updateTrackedItem() Q_Q(QQuickItemView); FxViewItem *item = currentItem; if (highlight) - item = highlight; + item = highlight.get(); trackedItem = item; if (trackedItem) diff --git a/src/quick/items/qquickitemview_p_p.h b/src/quick/items/qquickitemview_p_p.h index 1f08e4f2c9..81df3c67c6 100644 --- a/src/quick/items/qquickitemview_p_p.h +++ b/src/quick/items/qquickitemview_p_p.h @@ -279,7 +279,7 @@ public: QPauseAnimationJob bufferPause; QQmlComponent *highlightComponent; - FxViewItem *highlight; + std::unique_ptr<FxViewItem> highlight; int highlightRange; // enum value qreal highlightRangeStart; qreal highlightRangeEnd; diff --git a/src/quick/items/qquicklistview.cpp b/src/quick/items/qquicklistview.cpp index bdb3b1227f..1f538043ec 100644 --- a/src/quick/items/qquicklistview.cpp +++ b/src/quick/items/qquicklistview.cpp @@ -157,9 +157,9 @@ public: QQuickListView::HeaderPositioning headerPositioning; QQuickListView::FooterPositioning footerPositioning; - QSmoothedAnimation *highlightPosAnimator; - QSmoothedAnimation *highlightWidthAnimator; - QSmoothedAnimation *highlightHeightAnimator; + std::unique_ptr<QSmoothedAnimation> highlightPosAnimator; + std::unique_ptr<QSmoothedAnimation> highlightWidthAnimator; + std::unique_ptr<QSmoothedAnimation> highlightHeightAnimator; qreal highlightMoveVelocity; qreal highlightResizeVelocity; int highlightResizeDuration; @@ -202,11 +202,6 @@ public: { highlightMoveDuration = -1; //override default value set in base class } - ~QQuickListViewPrivate() { - delete highlightPosAnimator; - delete highlightWidthAnimator; - delete highlightHeightAnimator; - } friend class QQuickViewSection; @@ -979,14 +974,13 @@ void QQuickListViewPrivate::createHighlight(bool onDestruction) { bool changed = false; if (highlight) { - if (trackedItem == highlight) + if (trackedItem == highlight.get()) trackedItem = nullptr; - delete highlight; - highlight = nullptr; + highlight.reset(); - delete highlightPosAnimator; - delete highlightWidthAnimator; - delete highlightHeightAnimator; + highlightPosAnimator.reset(); + highlightWidthAnimator.reset(); + highlightHeightAnimator.reset(); highlightPosAnimator = nullptr; highlightWidthAnimator = nullptr; highlightHeightAnimator = nullptr; @@ -1001,7 +995,8 @@ void QQuickListViewPrivate::createHighlight(bool onDestruction) if (currentItem) { QQuickItem *item = createHighlightItem(); if (item) { - FxListItemSG *newHighlight = new FxListItemSG(item, q, true); + std::unique_ptr<FxListItemSG> newHighlight + = std::make_unique<FxListItemSG>(item, q, true); newHighlight->trackGeometry(true); if (autoHighlight) { @@ -1009,22 +1004,22 @@ void QQuickListViewPrivate::createHighlight(bool onDestruction) newHighlight->setPosition(static_cast<FxListItemSG*>(currentItem)->itemPosition()); } const QLatin1String posProp(orient == QQuickListView::Vertical ? "y" : "x"); - highlightPosAnimator = new QSmoothedAnimation; + highlightPosAnimator = std::make_unique<QSmoothedAnimation>(); highlightPosAnimator->target = QQmlProperty(item, posProp); highlightPosAnimator->velocity = highlightMoveVelocity; highlightPosAnimator->userDuration = highlightMoveDuration; - highlightWidthAnimator = new QSmoothedAnimation; + highlightWidthAnimator = std::make_unique<QSmoothedAnimation>(); highlightWidthAnimator->velocity = highlightResizeVelocity; highlightWidthAnimator->userDuration = highlightResizeDuration; highlightWidthAnimator->target = QQmlProperty(item, QStringLiteral("width")); - highlightHeightAnimator = new QSmoothedAnimation; + highlightHeightAnimator = std::make_unique<QSmoothedAnimation>(); highlightHeightAnimator->velocity = highlightResizeVelocity; highlightHeightAnimator->userDuration = highlightResizeDuration; highlightHeightAnimator->target = QQmlProperty(item, QStringLiteral("height")); - highlight = newHighlight; + highlight = std::move(newHighlight); changed = true; } } @@ -1064,8 +1059,10 @@ void QQuickListViewPrivate::updateHighlight() void QQuickListViewPrivate::resetHighlightPosition() { - if (highlight && currentItem) - static_cast<FxListItemSG*>(highlight)->setPosition(static_cast<FxListItemSG*>(currentItem)->itemPosition()); + if (highlight && currentItem) { + static_cast<FxListItemSG*>(highlight.get())->setPosition( + static_cast<FxListItemSG*>(currentItem)->itemPosition()); + } } bool QQuickListViewPrivate::movingFromHighlight() @@ -3393,7 +3390,7 @@ void QQuickListView::viewportMoved(Qt::Orientations orient) pos = viewPos + d->highlightRangeStart; if (pos != d->highlight->position()) { d->highlightPosAnimator->stop(); - static_cast<FxListItemSG*>(d->highlight)->setPosition(pos); + static_cast<FxListItemSG*>(d->highlight.get())->setPosition(pos); } else { d->updateHighlight(); } |
