diff options
author | Honglei Zhang <honglei.zhang@nokia.com> | 2011-11-30 11:36:40 (GMT) |
---|---|---|
committer | Honglei Zhang <honglei.zhang@nokia.com> | 2011-11-30 11:36:40 (GMT) |
commit | 9a5fb6bd5f0fb3b37897bf722e4cc1673309623c (patch) | |
tree | 3e7a2a4c9df49a9bc110f5dd24d7ed833ff8a2a4 | |
parent | 1ba0ce0cc1ab94f1397114ac8024a60dc7dfbdd8 (diff) | |
download | Qt-9a5fb6bd5f0fb3b37897bf722e4cc1673309623c.zip Qt-9a5fb6bd5f0fb3b37897bf722e4cc1673309623c.tar.gz Qt-9a5fb6bd5f0fb3b37897bf722e4cc1673309623c.tar.bz2 |
Fix sqlite driver memory eating due to close failure
If an ongoing query is not finalized before close function is
called, sqlite driver still tries to close the connection to
sqlite. In this case, sqlite reports an error to sqlite driver
which is not reported to the client. The failure in close causes
connection to sqlite unclosed and memory is not freed. This
fix tries to finalize all queries before close function is called.
The close function should succeed.
Task-number: QTBUG-16967
Reviewed-by: Charles Yin
-rw-r--r-- | src/sql/drivers/sqlite/qsql_sqlite.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qsqlquery/tst_qsqlquery.cpp | 49 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/sql/drivers/sqlite/qsql_sqlite.cpp b/src/sql/drivers/sqlite/qsql_sqlite.cpp index 8294a55..38e4a63 100644 --- a/src/sql/drivers/sqlite/qsql_sqlite.cpp +++ b/src/sql/drivers/sqlite/qsql_sqlite.cpp @@ -104,6 +104,7 @@ class QSQLiteDriverPrivate public: inline QSQLiteDriverPrivate() : access(0) {} sqlite3 *access; + QList <QSQLiteResult *> results; }; @@ -286,10 +287,12 @@ QSQLiteResult::QSQLiteResult(const QSQLiteDriver* db) { d = new QSQLiteResultPrivate(this); d->access = db->d->access; + db->d->results.append(this); } QSQLiteResult::~QSQLiteResult() { + qobject_cast<const QSQLiteDriver *>(driver())->d->results.removeOne(this); d->cleanup(); delete d; } @@ -553,6 +556,10 @@ bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, c void QSQLiteDriver::close() { if (isOpen()) { + foreach (QSQLiteResult *result, d->results) { + result->d->finalize(); + } + if (sqlite3_close(d->access) != SQLITE_OK) setLastError(qMakeError(d->access, tr("Error closing database"), QSqlError::ConnectionError)); diff --git a/tests/auto/qsqlquery/tst_qsqlquery.cpp b/tests/auto/qsqlquery/tst_qsqlquery.cpp index 3cbdb63..652a82e 100644 --- a/tests/auto/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/qsqlquery/tst_qsqlquery.cpp @@ -215,6 +215,8 @@ private slots: void QTBUG_14132(); void QTBUG_21884_data() { generic_data("QSQLITE"); } void QTBUG_21884(); + void QTBUG_16967_data() { generic_data("QSQLITE"); } + void QTBUG_16967(); //clean close void sqlite_constraint_data() { generic_data("QSQLITE"); } void sqlite_constraint(); @@ -3150,6 +3152,53 @@ void tst_QSqlQuery::QTBUG_21884() } } +/** + * This test case test sqlite driver close function. Sqlite driver should close cleanly + * even if there is still outstanding prepared statement. + */ +void tst_QSqlQuery::QTBUG_16967() +{ + QFETCH(QString, dbName); + { + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + db.close(); + QCOMPARE(db.lastError().type(), QSqlError::NoError); + } + { + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + QSqlQuery q(db); + q.prepare("CREATE TABLE t1 (id INTEGER PRIMARY KEY, str TEXT);"); + db.close(); + QCOMPARE(db.lastError().type(), QSqlError::NoError); + } + { + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + QSqlQuery q(db); + q.prepare("CREATE TABLE t1 (id INTEGER PRIMARY KEY, str TEXT);"); + q.exec(); + db.close(); + QCOMPARE(db.lastError().type(), QSqlError::NoError); + } + { + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + QSqlQuery q(db); + q.exec("INSERT INTO t1 (id, str) VALUES(1, \"test1\");"); + db.close(); + QCOMPARE(db.lastError().type(), QSqlError::NoError); + } + { + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + QSqlQuery q(db); + q.exec("SELECT * FROM t1;"); + db.close(); + QCOMPARE(db.lastError().type(), QSqlError::NoError); + } +} void tst_QSqlQuery::oraOCINumber() { |