SQLite Forum



(1) By Benedikt Rothe (BenediktRothe) on 2021-03-28 12:48:51 [link] [source]

SQLITE/Spatialite seems to leak ressources.

We face this problem in a bigger application, but it can be reproduced with the attached minimal program.

In the 537th run the program terminates when loading the mod_spatialite-DLL with this message:
Eine DLL-Initialisierungsroutine ist fehlgeschlagen.

* 64 Bit - Compiled with Debug-Mode
* Sqlite-Amalgam 3.35.2
* Prebuild Spatialite: mod_spatialite-5.0.1-win-amd64

As far as I can see, this problem is reproducable.
Does anybody have any advice?



#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>    

int main() {
   sqlite3* db;
   auto loadSpatialite= [&]() {
      char* errMsg = 0;
      sqlite3_enable_load_extension(db, 1);
      int rc = sqlite3_load_extension(db, "mod_spatialite", 0, &errMsg);
      if (rc != SQLITE_OK && errMsg != 0) {
         printf("ERROR ON LOAD EXTENSION\n%s", errMsg);
   const char* fName = "bsplinbf.sqlite";
   for (int i = 1; i < 1000; i++) {
      printf("Run: %d\n",i);
      int rc = sqlite3_open(fName, &db);



(2.1) By Larry Brasfield (larrybr) on 2021-03-28 17:54:39 edited from 2.0 in reply to 1 [link] [source]

As a preliminary matter, you have no basis to claim "seems to leak resources" when you call exit() upon encountering the sqlite3_load_extension() error return. If you closed the connection first, that would be different.

For a number of releases now, the SQLite library has blocked extension loading by default. See the current documentation for details.

(Edit added after post #3 to add:)

The code you posted compiles cleanly (as C++) and works without emitting error messages when I replace "mod_spatialite" with a literal which is the basename (without extension or directory components) of an extension findable in my PATH. So I think you should check to see if that is the case for your mod_spatialite. If it is, you might check whether it is built for the same machine type and has the extension commonly used for your platform.

(3) By anonymous on 2021-03-28 15:50:01 in reply to 2.0 [link] [source]

For a number of releases now, the SQLite library has blocked extension loading by default.

Wow. That is a really relevant comment ... considering the fact that the example code contains

sqlite3_enable_load_extension(db, 1);

(4) By Keith Medcalf (kmedcalf) on 2021-03-28 19:22:48 in reply to 1 [link] [source]

If I change the silly declaration of loadSpatialite into a "normal" function declaration (my compiler pukes on the sillyness as written), then on 64-bit Windows 10 with the current tip of trunk it explodes on iteration 547. I know not what the error is because it explodes before reporting the error so all you get is the standard Windows "Sorry about your defective program" dialog box, accompanied by no useful information at all.

Subsequent "runs" are the same -- which makes sense since the Operating System is responsible for "wiping the bottom clean" of a terminated process, so it is unlikely to get it wrong.

If I use a "properly written" extension (ie, one that I wrote or audited myself so I know it does not do anything untoward and wipes its bottom properly when the connection closes) there is no problem.

So it appears (to me) likely that the extension mod_spatialite (or one of its 13 dependencies) is allocating resources and not releasing them (not wiping its bottom properly) when the connection is closed.

(5) By Keith Medcalf (kmedcalf) on 2021-03-28 19:31:20 in reply to 4 [source]

And just in case it is load size dependent, my extension works fine though 100,000 iterations.

(6) By Benedikt Rothe (BenediktRothe) on 2021-03-28 21:07:00 in reply to 4 [link] [source]

So it appears (to me) likely that the extension mod_spatialite


The testprogram does not exit with an error, if I switch to SQLite 3.27.2 (but stick with mod_spatialite-5.0.1-win-amd64.)

I don't say, there's a bug in SQLite - but there seems to be a change in the way SQLite initializes/closes extension.

(7) By Keith Medcalf (kmedcalf) on 2021-03-28 22:00:38 in reply to 6 [link] [source]

So when it exits with an error, what is the error? Perhaps that may be helpful. I do not ever get an "exit with an error", I get an ABEND.

(8) By Benedikt Rothe (BenediktRothe) on 2021-03-29 05:16:57 in reply to 7 [link] [source]

Error occures when calling LoadLibraryW for mod_spatiallite

A guy from Spatialite will try o fix it: https://groups.google.com/g/spatialite-users/c/PX7PQ1_36mQ