From b1f11207044d0c693101665d6457984615e3c406 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 17 Mar 2021 17:09:10 +0100 Subject: Use unique_ptr to clarify ownership of QQuickDefaultClipNode objects The clang static analyzer warns in 3df1fff15a10a64372ed4f92ba05271f about a potential memory leak. While that particular claim is a false positive (the loop is always entered if sortedIndex is not empty), the re-use of the currentClipNode variable makes it hard to follow the object ownership, and there might still be a potential memory leak. Use std::unique_ptr to force explicit transfer of ownership, and get implicit destruction of objects not owned at the end of the scope. Change-Id: If826e1d81b92f1da60aae2262b628dcaaa2e592a Reviewed-by: Eskil Abrahamsen Blomfeldt (cherry picked from commit 02c6e7bc3aca42a188b772aa9794b919e60017e7) Reviewed-by: Qt Cherry-pick Bot --- src/quick/items/qquicktextnodeengine.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/quick/items/qquicktextnodeengine.cpp b/src/quick/items/qquicktextnodeengine.cpp index dd666416e8..38f199ecc0 100644 --- a/src/quick/items/qquicktextnodeengine.cpp +++ b/src/quick/items/qquicktextnodeengine.cpp @@ -257,7 +257,7 @@ void QQuickTextNodeEngine::processCurrentLine() QVarLengthArray pendingOverlines; QVarLengthArray pendingStrikeOuts; if (!sortedIndexes.isEmpty()) { - QQuickDefaultClipNode *currentClipNode = m_hasSelection ? new QQuickDefaultClipNode(QRectF()) : nullptr; + std::unique_ptr currentClipNode(m_hasSelection ? new QQuickDefaultClipNode(QRectF()) : nullptr); bool currentClipNodeUsed = false; for (int i=0; i<=sortedIndexes.size(); ++i) { BinaryTreeNode *node = nullptr; @@ -305,7 +305,7 @@ void QQuickTextNodeEngine::processCurrentLine() if (currentClipNode != nullptr) { if (!currentClipNodeUsed) { - delete currentClipNode; + currentClipNode.reset(); } else { currentClipNode->setIsRectangular(true); currentClipNode->setRect(currentRect); @@ -314,9 +314,9 @@ void QQuickTextNodeEngine::processCurrentLine() } if (node != nullptr && m_hasSelection) - currentClipNode = new QQuickDefaultClipNode(QRectF()); + currentClipNode.reset(new QQuickDefaultClipNode(QRectF())); else - currentClipNode = nullptr; + currentClipNode.reset(nullptr); currentClipNodeUsed = false; if (node != nullptr) { @@ -336,7 +336,7 @@ void QQuickTextNodeEngine::processCurrentLine() if (node != nullptr) { if (node->selectionState == Selected) { - node->clipNode = currentClipNode; + node->clipNode = currentClipNode.release(); currentClipNodeUsed = true; } -- cgit v1.2.3