From 67f7892958b11219da564e3db9bbb1376ce6d86f Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 8 Jun 2015 15:49:37 +0200 Subject: [PATCH] SocketAPI: Do not leak open DB or query We could leak it in case of errors. Or when the folder is removed. Make use of QSharedPointer to avoid leaks --- src/gui/socketapi.cpp | 13 ++++++------- src/gui/socketapi.h | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/gui/socketapi.cpp b/src/gui/socketapi.cpp index 1a7f64101..31c8d07a8 100644 --- a/src/gui/socketapi.cpp +++ b/src/gui/socketapi.cpp @@ -245,14 +245,14 @@ void SocketApi::slotUnregisterPath( const QString& alias ) broadcastMessage(QLatin1String("UNREGISTER_PATH"), f->path(), QString::null, true ); if( _dbQueries.contains(f)) { - SqlQuery *h = _dbQueries[f]; + auto h = _dbQueries[f]; if( h ) { h->finish(); } _dbQueries.remove(f); } if( _openDbs.contains(f) ) { - SqlDatabase *db = _openDbs[f]; + auto db = _openDbs[f]; if( db ) { db->close(); } @@ -476,7 +476,7 @@ SqlQuery* SocketApi::getSqlQuery( Folder *folder ) } if( _dbQueries.contains(folder) ) { - return _dbQueries[folder]; + return _dbQueries[folder].data(); } /* No valid sql query object yet for this folder */ @@ -487,21 +487,20 @@ SqlQuery* SocketApi::getSqlQuery( Folder *folder ) QFileInfo fi(dbFileName); if( fi.exists() ) { - SqlDatabase *db = new SqlDatabase; + auto db = QSharedPointer::create(); if( db && db->openReadOnly(dbFileName) ) { _openDbs.insert(folder, db); - SqlQuery *query = new SqlQuery(*db); + auto query = QSharedPointer::create(*db); rc = query->prepare(sql); if( rc != SQLITE_OK ) { - delete query; qDebug() << "Unable to prepare the query statement:" << rc; return 0; // do not insert into hash } _dbQueries.insert( folder, query); - return query; + return query.data(); } else { qDebug() << "Unable to open db" << dbFileName; } diff --git a/src/gui/socketapi.h b/src/gui/socketapi.h index 97f7b9ad1..c39c54006 100644 --- a/src/gui/socketapi.h +++ b/src/gui/socketapi.h @@ -85,8 +85,8 @@ private: QList _listeners; QLocalServer _localServer; c_strlist_t *_excludes; - QHash _dbQueries; - QHash _openDbs; + QHash> _dbQueries; + QHash> _openDbs; }; }