From b17755c46a04f828c5d5ce1c471e659c7fc2bb47 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 5 Jul 2019 22:13:46 +0200 Subject: [PATCH] Only register error log callback once at start of application Instead of registering the error log callback function every time a database file is opened, we now register it only once when the application is first started. This way we can avoid calling sqlite_shutdown which can have unintended side effects. See issue #1932. --- src/sqlitedb.cpp | 19 +++++++++++++------ src/sqlitedb.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index f7bfc2dd..4bd42fc8 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -87,6 +87,19 @@ static void sqlite_make_single_value(sqlite3_context* ctx, int num_arguments, sq }); } +DBBrowserDB::DBBrowserDB() : + _db(nullptr), + db_used(false), + isEncrypted(false), + isReadOnly(false), + dontCheckForStructureUpdates(false) +{ + // Register error log callback. This needs to be done before SQLite is first used + Callback::func = std::bind(&DBBrowserDB::errorLogCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + void (*log_callback)(void*, int, const char*) = static_cast(Callback::callback); + sqlite3_config(SQLITE_CONFIG_LOG, log_callback, nullptr); +} + void DBBrowserDB::collationNeeded(void* /*pData*/, sqlite3* /*db*/, int eTextRep, const char* sCollationName) { QString name(sCollationName); @@ -149,12 +162,6 @@ bool DBBrowserDB::open(const QString& db, bool readOnly) if(tryEncryptionSettings(db, &isEncrypted, cipherSettings) == false) return false; - // Register error log callback. We need to make sure SQLite is being shut down before calling sqlite3_config. - sqlite3_shutdown(); - Callback::func = std::bind(&DBBrowserDB::errorLogCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); - void (*log_callback)(void*, int, const char*) = static_cast(Callback::callback); - sqlite3_config(SQLITE_CONFIG_LOG, log_callback, nullptr); - // Open database file if(sqlite3_open_v2(db.toUtf8(), &_db, readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE, nullptr) != SQLITE_OK) { diff --git a/src/sqlitedb.h b/src/sqlitedb.h index fb2f3e28..4df1ab11 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -65,7 +65,7 @@ private: public: - explicit DBBrowserDB () : _db(nullptr), db_used(false), isEncrypted(false), isReadOnly(false), dontCheckForStructureUpdates(false) {} + explicit DBBrowserDB(); ~DBBrowserDB () override {} bool open(const QString& db, bool readOnly = false);