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.
This commit is contained in:
Martin Kleusberg
2019-07-05 22:13:46 +02:00
parent d6e4ff7c20
commit b17755c46a
2 changed files with 14 additions and 7 deletions

View File

@@ -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<void(void*, int, const char*)>::func = std::bind(&DBBrowserDB::errorLogCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
void (*log_callback)(void*, int, const char*) = static_cast<decltype(log_callback)>(Callback<void(void*, int, const char*)>::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<void(void*, int, const char*)>::func = std::bind(&DBBrowserDB::errorLogCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
void (*log_callback)(void*, int, const char*) = static_cast<decltype(log_callback)>(Callback<void(void*, int, const char*)>::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)
{

View File

@@ -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);