From b76cdb8017a2917a7aecd9dcb473a581b6da9108 Mon Sep 17 00:00:00 2001 From: uclaros Date: Wed, 5 Feb 2025 18:53:47 +0200 Subject: [PATCH] Plug leaks --- src/core/stac/qgsstaccontroller.cpp | 9 +++------ src/core/stac/qgsstacdataitems.cpp | 1 + src/core/stac/qgsstacparser.cpp | 30 ++++++++++++++++++++-------- src/gui/stac/qgsstacsourceselect.cpp | 8 ++++---- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/core/stac/qgsstaccontroller.cpp b/src/core/stac/qgsstaccontroller.cpp index df8d9ce7255..762f8ed574d 100644 --- a/src/core/stac/qgsstaccontroller.cpp +++ b/src/core/stac/qgsstaccontroller.cpp @@ -32,9 +32,11 @@ QgsStacController::~QgsStacController() { qDeleteAll( mReplies ); + qDeleteAll( mFetchedStacObjects ); + qDeleteAll( mFetchedItemCollections ); + qDeleteAll( mFetchedCollections ); } - int QgsStacController::fetchStacObjectAsync( const QUrl &url ) { QNetworkReply *reply = fetchAsync( url ); @@ -391,8 +393,3 @@ QgsStacItem *QgsStacController::openLocalItem( const QString &fileName ) const parser.setBaseUrl( fileName ); return parser.item(); } - - - - - diff --git a/src/core/stac/qgsstacdataitems.cpp b/src/core/stac/qgsstacdataitems.cpp index 638dca7d24c..08e0df860d0 100644 --- a/src/core/stac/qgsstacdataitems.cpp +++ b/src/core/stac/qgsstacdataitems.cpp @@ -202,6 +202,7 @@ void QgsStacItemItem::itemRequestFinished( int requestId, QString error ) } else { + delete object; mIconName = QStringLiteral( "/mIconDelete.svg" ); mName = error; } diff --git a/src/core/stac/qgsstacparser.cpp b/src/core/stac/qgsstacparser.cpp index 2e7ad657e5d..631834b82ab 100644 --- a/src/core/stac/qgsstacparser.cpp +++ b/src/core/stac/qgsstacparser.cpp @@ -484,18 +484,25 @@ QgsStacItemCollection *QgsStacParser::itemCollection() { QVector< QgsStacLink > links = parseLinks( mData.at( "links" ) ); - QVector< QgsStacItem * > items; + std::vector< std::unique_ptr > items; items.reserve( static_cast( mData.at( "features" ).size() ) ); for ( auto &item : mData.at( "features" ) ) { - QgsStacItem *i = parseItem( item ); + std::unique_ptr i( parseItem( item ) ); if ( i ) - items.append( i ); + items.emplace_back( i.release() ); } const int numberMatched = mData.contains( "numberMatched" ) ? mData["numberMatched"].get() : -1; - return new QgsStacItemCollection( items, links, numberMatched ); + QVector< QgsStacItem *> rawItems; + rawItems.reserve( items.size() ); + for ( std::unique_ptr &i : items ) + { + rawItems.append( i.release() ); + } + + return new QgsStacItemCollection( rawItems, links, numberMatched ); } catch ( nlohmann::json::exception &ex ) { @@ -511,18 +518,25 @@ QgsStacCollections *QgsStacParser::collections() { QVector< QgsStacLink > links = parseLinks( mData.at( "links" ) ); - QVector< QgsStacCollection * > cols; + std::vector< std::unique_ptr > cols; cols.reserve( static_cast( mData.at( "collections" ).size() ) ); for ( auto &col : mData.at( "collections" ) ) { - QgsStacCollection *c = parseCollection( col ); + std::unique_ptr c( parseCollection( col ) ); if ( c ) - cols.append( c ); + cols.emplace_back( c.release() ); } const int numberMatched = mData.contains( "numberMatched" ) ? mData["numberMatched"].get() : -1; - return new QgsStacCollections( cols, links, numberMatched ); + QVector< QgsStacCollection * > rawCols; + rawCols.reserve( cols.size() ); + for ( std::unique_ptr &c : cols ) + { + rawCols.append( c.release() ); + } + + return new QgsStacCollections( rawCols, links, numberMatched ); } catch ( nlohmann::json::exception &ex ) { diff --git a/src/gui/stac/qgsstacsourceselect.cpp b/src/gui/stac/qgsstacsourceselect.cpp index 7801cc1788c..1c361d214ff 100644 --- a/src/gui/stac/qgsstacsourceselect.cpp +++ b/src/gui/stac/qgsstacsourceselect.cpp @@ -277,8 +277,8 @@ void QgsStacSourceSelect::cmbConnections_currentTextChanged( const QString &text void QgsStacSourceSelect::onStacObjectRequestFinished( int requestId, QString error ) { QgsDebugMsgLevel( QStringLiteral( "Finished object request %1" ).arg( requestId ), 2 ); - QgsStacObject *obj = mStac->takeStacObject( requestId ); - QgsStacCatalog *cat = dynamic_cast( obj ); + std::unique_ptr obj( mStac->takeStacObject( requestId ) ); + QgsStacCatalog *cat = dynamic_cast( obj.get() ); if ( !cat ) { @@ -321,7 +321,7 @@ void QgsStacSourceSelect::onStacObjectRequestFinished( int requestId, QString er void QgsStacSourceSelect::onCollectionsRequestFinished( int requestId, QString error ) { QgsDebugMsgLevel( QStringLiteral( "Finished collections request %1" ).arg( requestId ), 2 ); - QgsStacCollections *cols = mStac->takeCollections( requestId ); + std::unique_ptr cols( mStac->takeCollections( requestId ) ); if ( !cols ) { @@ -343,7 +343,7 @@ void QgsStacSourceSelect::onCollectionsRequestFinished( int requestId, QString e void QgsStacSourceSelect::onItemCollectionRequestFinished( int requestId, QString error ) { QgsDebugMsgLevel( QStringLiteral( "Finished item collection request %1" ).arg( requestId ), 2 ); - QgsStacItemCollection *col = mStac->takeItemCollection( requestId ); + std::unique_ptr col( mStac->takeItemCollection( requestId ) ); if ( !col ) {