Hello everyone,
I am currently facing a memory leak issue when performing continuous queries on a SpatiaLite database from my C++ program. Let me describe my setup and problem in detail.
In the main thread, I open the database and initialize SpatiaLite using the following code:
int rc = sqlite3_open_v2(dbName, &db, SQLITE_OPEN_READONLY, nullptr);
if (rc == SQLITE_OK) {
this->dbExit = true;
}
if (enableExtension) {
spatialite_init_geos();
}
cache = spatialite_alloc_connection();
spatialite_init_ex(db, cache, 0);
However, the actual queries are executed in a newly created thread, for example:
std::thread drawThread([this, globeControl, corners]() {
std::vector<std::vector<int>> array;
for (const auto &layerValue : gestureFeedbackCase->layerCasesMap) {
layerValue.second->startControl(globeControl, corners);
}
update(globeControl, globeControl->getVisibleCorners());
});
drawThread.detach();
Inside the startControl method, I query the database like this:
std::vector<Airport> airports;
if (this->dbExit) {
std::string sql = "..."; // simplified for clarity
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
rc = sqlite3_bind_text(stmt, 1, "Y",-1,SQLITE_STATIC);
rc = sqlite3_bind_text(stmt, 2, "MA",-1,SQLITE_STATIC);
if (rc != SQLITE_OK) {
return airports;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
Airport airport;
fillAirportFromSql(stmt, airport);
airports.push_back(airport);
}
sqlite3_finalize(stmt);
}
return airports;
Since these queries run continuously, I very quickly encounter memory exhaustion.
When I checked the SpatiaLite documentation, I found that this approach may cause the spatial cache not to be released. On the other hand, the documentation also says that spatialite_cleanup_ex() should only be used when closing the database, which does not seem realistic for my use case because I need to keep the database connection open for ongoing queries.
So my questions are:
What is the actual root cause of the memory leak in my scenario?
For my use case (continuous queries from a long-lived connection), what is the correct way to use SpatiaLite to avoid memory leaks?
Any guidance would be greatly appreciated.
Thanks in advance,
by may
Thank you very much for your reply!
Your suggestions are extremely valuable to us. I will next try to use a single thread for both database connection and query operations, and also make use of the memory analysis tools you mentioned.
Lastly, please allow me to express my sincere admiration for your work — SpatiaLite is undoubtedly outstanding, and in my field (civil aviation) it has been of tremendous help to us.
by may