sqlite unexpected access violations lazarus / 63-Database ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:02 After in another post I got no immediate suggestion, I created a test program in order to isolate the problem and I now post it here. original post: https://forum.lazarus.freepascal.org/index.php/topic,66360.0.html So, consider the attached zip with software and db. I see random access violations like this: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [10:47:02] mep@mep-development:~/tmp/2024-02-26-threads-and-db$ ./project1 Internal error unhandled 1 cycle 0: Access violationInternal error unhandled 1 cycle 1: Access violationInternal error unhandled 1 cycle 2: Access violation"SQLite3","3034001","3.34.1","libsqlite3.so","3034001"Heap dump by heaptrc unit of /home/mep/tmp/2024-02-26-threads-and- db/project1192262 memory blocks allocated : 16057951/16278136192262 memory blocks freed     : 16057951/162781360 unfreed memory blocks : 0True heap size : 131072True free heap : 131072[10:53:01] mep@mep- development:~/tmp/2024-02-26-threads-and-db$ ./project1 Internal error unhandled 1 cycle 0: Access violationInternal error unhandled 1 cycle 1: Access violationInternal error unhandled 1 cycle 2: Access violationInternal error unhandled 1 cycle 3: Access violation"SQLite3","3034001","3.34.1","libsqlite3.so","3034001"Heap dump by heaptrc unit of /home/mep/tmp/2024-02-26-threads-and- db/project1192192 memory blocks allocated : 16052122/16272224192192 memory blocks freed     : 16052122/162722240 unfreed memory blocks : 0True heap size : 131072True free heap : 131072  I tried also with latest sqlite 3.45.1. I don't get what is going on, because despite the existence of multiple threads, they all work on separate databases, and connection/transaction/query objects are always created/destroyed. Thanks for help. ---------------------------------------------------------------------- TRon · 2024-02-29 10:10 In which mode is your sqlite library compiled ? see also here edit: btw you can set the OpenFlags property of TSQLite3Connection. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:26 Quote from: TRon on February 29, 2024, 11:10:19 amIn which mode is your sqlite library compiled ? see also here The version shown in the post is 3.34.1, which is the one shipped with debian 11.5. I don't know exactly how they compile it but I would say that they compile with defaults from sqlite source code. I also downloaded the latest sqlite 3.45.1 and I compiled default, only adding the debug messages. I get same result in both cases of usage. Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [11:21:04] mep@mep-development:~/dev/src/c/y_3rd-party/SQLite$ cat sqlite- configure.sh#!/bin/sh rm -rf sqlite-build;mkdir sqlite-build;cd sqlite-build;../sqlite/configure --enable-debugcd ..;[11:21:08] mep@mep-development:~/dev/src/c/y_3rd-party/SQLite$ cat sqlite- build.sh #!/bin/sh cd sqlite-build;make all;cd ..;[11:21:12] mep@mep- development:~/dev/src/c/y_3rd-party/SQLite$ ./sqlite-configure.sh; ./sqlite-build.sh checking build system type... x86_64-pc-linux- gnuchecking host system type... x86_64-pc-linux-gnuchecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for a sed that does not truncate output... /usr/bin/sedchecking for grep that handles long lines and -e... /usr/bin/grepchecking for egrep... /usr/bin/grep -Echecking for fgrep... /usr/bin/grep -Fchecking for ld used by gcc... /usr/bin/ldchecking if the linker (/usr/bin/ld) is GNU ld... yeschecking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -Bchecking the name lister (/usr/bin/nm -B) interface... BSD nmchecking whether ln -s works... yeschecking the maximum length of command line arguments... 1572864checking whether the shell understands some XSI constructs... yeschecking whether the shell understands "+="... yeschecking for /usr/bin/ld option to reload object files... -rchecking for objdump... objdumpchecking how to recognize dependent libraries... pass_allchecking for ar... archecking for strip... stripchecking for ranlib... ranlibchecking command to parse /usr/bin/nm -B output from gcc object... okchecking how to run the C preprocessor... gcc -Echecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking for dlfcn.h... yeschecking for objdir... .libschecking if gcc supports -fno-rtti -fno-exceptions... nochecking for gcc option to produce PIC... -fPIC -DPICchecking if gcc PIC flag -fPIC -DPIC works... yeschecking if gcc static flag -static works... yeschecking if gcc supports -c -o file.o... yeschecking if gcc supports -c -o file.o... (cached) yeschecking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yeschecking whether -lc should be explicitly linked in... nochecking dynamic linker characteristics... GNU/Linux ld.sochecking how to hardcode library paths into programs... immediatechecking whether stripping libraries is possible... yeschecking if libtool supports shared libraries... yeschecking whether to build shared libraries... yeschecking whether to build static libraries... yeschecking for a BSD-compatible install... /usr/bin/install -cchecking for special C compiler options needed for large files... nochecking for _FILE_OFFSET_BITS value needed for large files... nochecking for int8_t... yeschecking for int16_t... yeschecking for int32_t... yeschecking for int64_t... yeschecking for intptr_t... yeschecking for uint8_t... yeschecking for uint16_t... yeschecking for uint32_t... yeschecking for uint64_t... yeschecking for uintptr_t... yeschecking for sys/types.h... (cached) yeschecking for stdlib.h... (cached) yeschecking for stdint.h... (cached) yeschecking for inttypes.h... (cached) yeschecking malloc.h usability... yeschecking malloc.h presence... yeschecking for malloc.h... yeschecking for fdatasync... yeschecking for gmtime_r... yeschecking for isnan... yeschecking for localtime_r... yeschecking for localtime_s... nochecking for malloc_usable_size... yeschecking for strchrnul... yeschecking for usleep... yeschecking for utime... yeschecking for pread... yeschecking for pread64... yeschecking for pwrite... yeschecking for pwrite64... yeschecking for tclsh8.7... nochecking for tclsh8.6... tclsh8.6configure: Version set to 3.45configure: Release set to 3.45.1checking for WASI SDK directory... nochecking whether to support threadsafe operation... yeschecking for library containing pthread_create... -lpthreadchecking for library containing pthread_mutexattr_init... none requiredchecking whether to support shared library linked as release mode or not... nochecking whether to use an in-ram database for temporary tables... nochecking if executables have the .exe suffix... unknownchecking for Tcl configuration... configure: WARNING: Can't find Tcl configuration definitionsconfigure: WARNING: *** Without Tcl the regression tests cannot be executed ***configure: WARNING: *** Consider using --with- tcl=... to define location of Tcl ***checking for library containing readline... nochecking for library containing tgetent... nochecking for readline in -lreadline... nochecking readline.h usability... nochecking readline.h presence... nochecking for readline.h... nochecking for /usr/include/readline.h... nochecking for /usr/include/readline/readline.h... nochecking for /usr/local/include/readline.h... nochecking for /usr/local/include/readline/readline.h... nochecking for /usr/local/readline/include/readline.h... nochecking for /usr/local/readline/include/readline/readline.h... nochecking for /usr/contrib/include/readline.h... nochecking for /usr/contrib/include/readline/readline.h... nochecking for /mingw/include/readline.h... nochecking for /mingw/include/readline/readline.h... nonot using linenoisechecking for library containing fdatasync... none requiredchecking build type... debugchecking zlib.h usability... yeschecking zlib.h presence... yeschecking for zlib.h... yeschecking for library containing deflate... -lzchecking for library containing dlopen... -ldlchecking whether to support math functions... yeschecking for library containing ceil... -lmchecking whether to support JSON functions... yeschecking whether to support MEMSYS5... nochecking whether to support MEMSYS3... nochecking whether to support FTS3... nochecking whether to support FTS4... nochecking whether to support FTS5... nochecking whether to support LIMIT on UPDATE and DELETE statements... nochecking whether to support GEOPOLY... nochecking whether to support RTREE... nochecking whether to support SESSION... noconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating sqlite3.pcconfig.status: creating sqlite_cfg.hconfig.status: executing libtool commands [...] gcc  [...] -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite -DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0 -Wall -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_MATH_FUNCTIONS  -DSQLITE_HAVE_ZLIB=1  -DSQLITE_TEMP_STORE=1 -c sqlite3.c[11:24:21] mep@mep-development:~/dev/src/c/y_3rd-party/SQLite$ ---------------------------------------------------------------------- TRon · 2024-02-29 10:30 Quote from: Чебурашка on February 29, 2024, 11:26:03 amI also downloaded the latest sqlite 3.45.1 and I compiled default, only adding the debug messages. then it seems to be on by default: Quote cc  [...] -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite -DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0 -Wall -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_MATH_FUNCTIONS  -DSQLITE_HAVE_ZLIB=1  -DSQLITE_TEMP_STORE=1 -c sqlite3.c Forget the above, the option -DSQLITE_THREADSAFE=1 is for serialized. It needs to have the number 2. see also https://www.sqlite.org/compile.html#threadsafe QuoteI get same result in both cases of usage. I am testing with default form my distro: 3.40.1 64 bit and have similar results. I am fiddling with openflags now. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:32 Quote from: TRon on February 29, 2024, 11:10:19 amedit: btw you can set the OpenFlags property of TSQLite3Connection. As I understand this is necessary when two threads use the same connection. In my case each thread uses it's own db file, connection, transaction, query. Thanks for helping, I am going mad on this thing. ---------------------------------------------------------------------- TRon · 2024-02-29 10:33 Please re-read my previous post Чебурашка as I have edited it. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:38 Quote from: TRon on February 29, 2024, 11:33:56 amPlease re-read my previous post Чебурашка as I have edited it. I saw, but as I told, unless I missed something, this would be a problem when multiple threads concurrently access the same connection (then therefore the same database). And this is absolutely not my case. Maybe I am missing something then? I will try recompiling with 2. ---------------------------------------------------------------------- TRon · 2024-02-29 10:41 Quote from: Чебурашка on February 29, 2024, 11:38:12 amI saw, but as I told, unless I missed something, this would be a problem when multiple threads concurrently access the same connection (then therefore the same database). That is not clear for me from reading the documentation. Nevertheless the library itself is just a single library and that is only opened once in your application (actually sqlitedyn does this behind the scenes). Note that having configured everything as correct for sqlite itself it does not mean that the code used in FPC is threadsafe. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:47 Quote from: TRon on February 29, 2024, 11:41:10 amThat is not clear for me from reading the documentation. Nevertheless the library itself is just a single library and that is only opened once in your application (actually sqlitedyn does this behind the scenes). I checked the sqlite docs and for what I can get, it seems that "-DSQLITE_THREADSAFE=1" is the right gcc option. From thier website: QuoteUse the SQLITE_THREADSAFE compile-time parameter to select the threading mode. If no SQLITE_THREADSAFE compile-time parameter is present, then serialized mode is used. This can be made explicit with -DSQLITE_THREADSAFE=1. With -DSQLITE_THREADSAFE=0 the threading mode is single-thread. With -DSQLITE_THREADSAFE=2 the threading mode is multi-thread. Quote from: TRon on February 29, 2024, 11:41:10 amNote that having configured everything as correct for sqlite itself it does not mean that the code used in FPC is threadsafe. Ops... ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:49 Quote from: Чебурашка on February 29, 2024, 11:38:12 amI will try recompiling with 2. I added to sqlite/configure script the option --enable-threadsafe and I get the same "-DSQLITE_THREADSAFE=1" so this is the right option for threadsafeness. ---------------------------------------------------------------------- Zvoni · 2024-02-29 10:50 Quote from: TRon on February 29, 2024, 11:30:10 amForget the above, the option -DSQLITE_THREADSAFE=1 is for serialized. It needs to have the number 2. see also https://www.sqlite.org/compile.html#threadsafe Eh? Would have to disagree. From the same page you cited: Quote When SQLite has been compiled with SQLITE_THREADSAFE=1 or SQLITE_THREADSAFE=2 then the threading mode can be altered at run-time using the sqlite3_config() interface together with one of these verbs:     SQLITE_CONFIG_SINGLETHREAD     SQLITE_CONFIG_MULTITHREAD     SQLITE_CONFIG_SERIALIZED The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags to sqlite3_open_v2() can also be used to adjust the threading mode of individual database connections at run-time. sqlite3_config() is defined in sqlite3.inc, but not exposed in TSQlite3Conn ---------------------------------------------------------------------- TRon · 2024-02-29 10:51 Quote from: Чебурашка on February 29, 2024, 11:47:06 amQuote from: TRon on February 29, 2024, 11:41:10 amNote that having configured everything as correct for sqlite itself it does not mean that the code used in FPC is threadsafe. Ops... For the record, not saying sqlite code in the package wasn't written threadsafe just that it does not mean that because the code exists it is threadsafe by default ---------------------------------------------------------------------- Чебурашка · 2024-02-29 10:55 Quote from: TRon on February 29, 2024, 11:51:59 amQuote from: Чебурашка on February 29, 2024, 11:47:06 amQuote from: TRon on February 29, 2024, 11:41:10 amNote that having configured everything as correct for sqlite itself it does not mean that the code used in FPC is threadsafe. Ops... For the record, not saying sqlite code in the package wasn't written threadsafe just that it does not mean that because the code exists it is threadsafe by default  I confirm that all this started to happen to me when I started having 3 databases, accessed by 2 threads like this: thread 1 goes on db 1 and db 2, thread 2 goes on db 3. Before I had only one thread accessing db 1 and db 2, and no error was occurring. ---------------------------------------------------------------------- TRon · 2024-02-29 10:55 Quote from: Zvoni on February 29, 2024, 11:50:39 amQuote from: TRon on February 29, 2024, 11:30:10 amForget the above, the option -DSQLITE_THREADSAFE=1 is for serialized. It needs to have the number 2. see also https://www.sqlite.org/compile.html#threadsafe Eh? Would have to disagree. You are correct Zvoni. Thank you for the correction. I completely missed "Note that when SQLite is compiled with SQLITE_THREADSAFE=0, the code to make SQLite threadsafe is omitted from the build. When this occurs, it is impossible to change the threading mode at start-time or run-time."  Ok, but do you agree that when our sqlite implementation would use open_v2 that then there is no need to use config but can use the flags instead ? ---------------------------------------------------------------------- TRon · 2024-02-29 11:19 Ok, call me crazy.... I just added: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   InitialiseSQLite;  writeln('is sqlite threadsafe : ', sqlite3_threadsafe); As first two lines in the RunTheTest procedure and that seems to have done the trick ? Is that really because the main program thread does not have an instance of the library ? ---------------------------------------------------------------------- Чебурашка · 2024-02-29 11:28 I analized a little the internals of our sqlite software, and I modified my software so that, while the tests are running there a dummy connection to another db that prevents the RefCount mechanism to load/unload the sqlite library every time connection is create/destroyed. Problems disappear, and also changing the number of concurent threds to 100 there is no error. Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [12:27:33] mep@mep-development:~/tmp/2024-02-26-threads-and-db$ ./project1 "SQLit e3","3034001","3.34.1","libsqlite3.so","3034001""SQLite3","3034001","3 .34.1","libsqlite3.so","3034001"Heap dump by heaptrc unit of /home/mep/tmp/2024-02-26-threads-and-db/project19611865 memory blocks allocated : 803625179/8144651049611865 memory blocks freed     : 803625179/8144651040 unfreed memory blocks : 0True heap size : 131072True free heap : 131072[ Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  cthreads,Classes, SysUtils,  sqlite3conn, sqldb, FileUtil,   Unit1; // template-sqlite_db is a pre-existing sqlite db with only one table://// CREATE TABLE Table1// (//   id integer,//   v1 varchar(255),//   text1 TEXT// ); var  connection: TSQLite3Connection;begin  connection := TSQLite3Connection.Create(nil);  connection.DatabaseName := 'another- sqlite_db';  connection.Connected := True;  Writeln(connection.GetConnectionInfo(TConnInfoType.citAll));    RunTheTest();    connection.Connected := False;  connection.Destroy();end.  The problem is in the fpc sqlite software. ---------------------------------------------------------------------- TRon · 2024-02-29 11:44 @Чебурашка: I am not 100% convinced that the problem is in the sqlite components/package implementation. It might be intended behaviour but I do not know. That question should probably be answered by someone who implemented the classes. At least opening the library in the main program thread solves your issue, so that is a relative easy fix do you agree ? ---------------------------------------------------------------------- Чебурашка · 2024-02-29 12:10 Quote from: TRon on February 29, 2024, 12:44:15 pm@Чебурашка: I am not 100% convinced that the problem is in the sqlite components/package implementation. It might be intended behaviour but I do not know. That question should probably be answered by someone who implemented the classes. At least opening the library in the main program thread solves your issue, so that is a relative easy fix do you agree ? The problem is related to the libsqlite3.so load/unload mechanism, that makes use of RefCount and InterlockedIncrement. I don't know exactly why but this is what happens when, like me one creates and destroys the connection obejcts every time they are used (btw it is not enough to create/destroy the connection obecjt, also Connected := True is necessary, because you have to go though the DoInternalConnect/DoInternalDisconnect couple). The idea of keeping a connection created and opened is just a ugly workaround because I happened to come across this problem just because my threads are doing free wheel operations, but having threads doing operations just seldom does only make more unlikely the problem to happen, which is even worse because one can have an AV in just one year of execution and that would be extremely difficult to interpret (like all the concurrence problems of the world). ---------------------------------------------------------------------- TRon · 2024-02-29 12:18 Quote from: Чебурашка on February 29, 2024, 01:10:34 pmThe problem is related to the libsqlite3.so load/unload mechanism, that makes use of RefCount and InterlockedIncrement. Yes, that was clear from the results and the source-code. For/to me the implementation looks like it was implemented as intended. (again I can not tell for sure simply because I did not implement the classes  ) QuoteThe idea of keeping a connection created and opened is just a ugly workaround because I happened to come across this problem just because my threads are doing free wheel operations, but having threads doing operations just seldom does only make more unlikely the problem to happen, which is even worse because one can have an AV in just one year of execution and that would be extremely difficult to interpret (like all the concurrence problems of the world). Yes, but it is not about a connection. The connection makes sure that the sqlite library is opened in the main thread (and stays open during your RunTheTest procedure) and has such has a main program thread instance. I solved it this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..uses  sqlite3dyn;..begin  InitializeSQLite;  try    writeln('sqlite version : ', sqlite3_libversion);    writeln('is sqlite threadsafe : ', sqlite3_threadsafe);     RunTheTest();  finally    ReleaseSQLite;  end;end; ---------------------------------------------------------------------- Zvoni · 2024-02-29 12:25 You do realize, that instead of creating a "dummy"-Database (on Harddisk) as a workaround, you could use an "InMemory"-Database for that? ---------------------------------------------------------------------- Чебурашка · 2024-02-29 12:25 Quote from: TRon on February 29, 2024, 01:18:23 pmI solved it this way: I just tried the same. It it like you say, not needed to open the connection, but just InitializeSQLite/ReleaseSQLite is enough. Unfortunately InitializeSQLite is a deprecated method and therefore it might be removed in future. Also it should be invesitigated why using the library like in the example raises the access violations, because the feeling is that who wrote the fp code wanted to implement a threadsafe refcounting mechanism. Why use interlockedincrement otherwise? ---------------------------------------------------------------------- TRon · 2024-02-29 12:27 Quote from: Чебурашка on February 29, 2024, 01:25:23 pmUnfortunately InitializeSQLite is a deprecated method and therefore it might be removed in future. InitialiseSQLite is indeed  QuoteAlso it should be invesitigated why using the library like in the example raises the access violations, because the feeling is that who wrote the fp code wanted to implement a threadsafe refcounting mechanism. Why use interlockedincrement otherwise? Unfortunately I can not answer that question. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 12:28 Quote from: Zvoni on February 29, 2024, 01:25:12 pmYou do realize, that instead of creating a "dummy"-Database (on Harddisk) as a workaround, you could use an "InMemory"-Database for that? Please see the following posts, it emerges that it is not even necessary to create dummy databases, just do a Init/Release of the library. Anyway thanks for the suggestion, even if I don't see a significant advantage in doing the dummy db in memory rather than on the filesystem. ---------------------------------------------------------------------- Zvoni · 2024-02-29 12:30 Quote from: Чебурашка on February 29, 2024, 01:25:23 pmUnfortunately InitializeSQLite is a deprecated method and therefore it might be removed in future. Wrong! Look closely From sqlite3.inc Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function InitializeSqliteANSI(const LibraryName: AnsiString = ''): Integer; //needed as TLibraryLoadFunctionfunction InitializeSqlite(const LibraryName: UnicodeString = ''): Integer;function TryInitializeSqlite(const LibraryName: Unicodestring = ''): Integer;procedure ReleaseSqlite; //needed as TLibraryUnLoadFunction function InitialiseSQLite: Integer; deprecated;function InitialiseSQLite(const LibraryName: UnicodeString): Integer; deprecated;  Quotefunction InitializeSqlite(const LibraryName: UnicodeString = ''): Integer; function InitialiseSQLite: Integer; deprecated; ---------------------------------------------------------------------- Чебурашка · 2024-02-29 12:35 Quote from: Zvoni on February 29, 2024, 01:30:30 pmWrong! Thanks I didn't notice the difference, prob someone decided to force the American version, as for what I see British English uses also Initialise. https://www.oxfordlearnersdictionaries.com/definition/english/initiali ze https://www.wordreference.com/enit/initialize https://dictionary.cambridge.org/it/dizionario/inglese/initialize But Ok, lets not diverge from the topic, which is the most important thing for the fp-sqlite users ---------------------------------------------------------------------- Zvoni · 2024-02-29 12:38 Quote from: Чебурашка on February 29, 2024, 01:35:54 pmQuote from: Zvoni on February 29, 2024, 01:30:30 pmWrong! Thanks I didn't notice the difference, prob someone decided to force the American version, as for what I see British English uses also Initialise. https://www.oxfordlearnersdictionaries.com/definition/english/initiali ze https://www.wordreference.com/enit/initialize Yeah, but i doubt it's going to be removed sqlite3.inc Quotefunction InitialiseSQLite:integer; begin   result:=InitializeSqlite(SQLiteDefaultLibrary); end; ---------------------------------------------------------------------- TRon · 2024-02-29 12:41 Quote from: Zvoni on February 29, 2024, 01:38:30 pmYeah, but i doubt it's going to be removed Nope, because ... consistency  (actually I meant to link to this but there does not seem to be a individual link to initialization and finalization) ---------------------------------------------------------------------- TRon · 2024-02-29 12:49 Quote from: Чебурашка on February 29, 2024, 01:35:54 pmBut Ok, lets not diverge from the topic, which is the most important thing for the fp-sqlite users If you have strong feelings with regards the implementation being wrong then you can always opt to report the issue as a bug though I would advise to wait a little (a couple of days or so) in case a more knowledgeable person might be able to shed a light. ---------------------------------------------------------------------- Чебурашка · 2024-02-29 13:26 Quote from: TRon on February 29, 2024, 01:49:52 pmIf you ... https://gitlab.com/freepascal.org/fpc/source/-/issues/40675 ---------------------------------------------------------------------- Чебурашка · 2024-02-29 14:54 Quote from: Чебурашка on February 29, 2024, 02:26:22 pmQuote from: TRon on February 29, 2024, 01:49:52 pmIf you ... https://gitlab.com/freepascal.org/fpc/source/-/issues/40675 Closed! ---------------------------------------------------------------------- Чебурашка · 2024-02-29 18:43 For the whom this matters: The answer to the issue opened in gitlab is "not a bug". It is normal that db components are not thread safe and the previously mentioned workaround (initialize/finalize-sqlite) is the solution to be used. Thank you all for the support and the fpc levels for the answer. I still don't understand why sqlite3dyn uses interlockedincrement to manipulate the refcount value, if the db components are not supposed to be thread-safe but it is not a problem, I am fine with the indication of using initialize/finalize. ---------------------------------------------------------------------- TRon · 2024-02-29 21:50 Thank you for the updates Чебурашка ---------------------------------------------------------------------- Чебурашка · 2024-03-06 14:33 Why the initialization process of the postgresql wrapper is made using critical sections, while the one of sqlite not? At the end when are talking in both cases of loading a dll, connecting the exported methods, using a kind of refcount. The two processes that have (basically) the same goal and the same way to achieve it, but they are made differently. Is there a reason that I don't get? Thanks for any explanation.