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
This commit is contained in:
Olivier Goffart 2015-06-08 15:49:37 +02:00
parent 6df7551bdd
commit 67f7892958
2 changed files with 8 additions and 9 deletions

View file

@ -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<SqlDatabase>::create();
if( db && db->openReadOnly(dbFileName) ) {
_openDbs.insert(folder, db);
SqlQuery *query = new SqlQuery(*db);
auto query = QSharedPointer<SqlQuery>::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;
}

View file

@ -85,8 +85,8 @@ private:
QList<QLocalSocket*> _listeners;
QLocalServer _localServer;
c_strlist_t *_excludes;
QHash<Folder*, SqlQuery*> _dbQueries;
QHash<Folder*, SqlDatabase*> _openDbs;
QHash<Folder*, QSharedPointer<SqlQuery>> _dbQueries;
QHash<Folder*, QSharedPointer<SqlDatabase>> _openDbs;
};
}