Using Custom SQLite Functions with Qt

I am trying to create a custom function for the SQLite database that I use with Qt. I found information on how to create a function and it seems to work correctly on x86 system.

Instead, it seems to fail with segfault on the ARM device. This is the code I wrote:

static bool createSQLiteFunctions(const QSqlDatabase& db) { // Get handle to the driver and check it is both valid and refers to SQLite3. QVariant v = db.driver()->handle(); if (!v.isValid() || qstrcmp(v.typeName(), "sqlite3*") != 0) { LOG_WARNING("Cannot get a sqlite3 handle to the driver."); return false; } // Create a handler and attach functions. sqlite3* handler = *static_cast<sqlite3**>(v.data()); if (!handler) { LOG_WARNING("Cannot get a sqlite3 handler."); return false; } // Check validity of the state. if (!db.isValid()) { LOG_ERROR("Cannot create SQLite custom functions: db object is not valid."); return false; } if (!db.isOpen()) { LOG_ERROR("Cannot create SQLite custom functions: db object is not open."); return false; } if (sqlite3_create_function(handler, "_deleteFile", 1, SQLITE_ANY, 0, &_sqlite3DeleteFile, 0, 0)) LOG_ERROR("Cannot create SQLite functions: sqlite3_create_function failed."); return true; } 

The db object is created as a member of another object that calls this function in the constructor where the connection to db is established (several instances can be created at the same time, but sqlite3 is compiled using the thread-safe option). It seems that the error log is not printed, but the sqlite3_create_function function gives segfault. If I remove the createSQLiteFunctions call, everything will be fine. Any idea why the result is segfault? Thank you

+2
source share
2 answers

If it does not work in ARM, perhaps something needs to be done with compilation (which sqlite3 do you use?) Or the callback function itself (_sqlite3DeleteFile). How it is determined, where it is located / connected. Depending on the ARM (which ARM you are using), the processor can be very picky with alignment. Check the map file, perhaps the MMU configuration, ...?

By the way: I would prefer to leave const as we modify db.

 static bool createSQLiteFunctions(/*const*/ QSqlDatabase& db) 

Can you debug a step in sqlite3_create_function code? Can you create a trace?

+1
source

The QSql driver for sqlite3 can be linked statically to sqlite3 in the Qt library (using the sqlite3 version of the Qt repositories) or dynamically use the version of sqlite3 present on your system (i.e. sysroot when you cross-compile for ARM).

The problem occurs when the version of sqlite3 that your application is associated with is different from the version associated with Qt, because it causes problems, for example, with static variables that are differently initialized in the two versions.

This problem is visible only when calling sqlite3 native functions on a descriptor derived from Qt, everything works fine as long as only QSqlDatabase functions are called, because it uses only the version of sqlite3 statically linked to Qt.

Check the Qt configure parameter "system-sqlite", also keep in mind that this option does not work in some versions of Qt (see commit ced4d167a25b in the qtbase repository).

+1
source

Source: https://habr.com/ru/post/890115/


All Articles