A few days ago, I’ve got an issue with spatialite - mod_spatialite-4.3.0a-win-amd64. After googling for a solution, I found some people with the very same issue and no solution. Then, I decided to try to figure it out.
Symptoms
I was developing a tool to work with spatial data and when I tried to use spatialite from a C# application I got the following error:
1
Unhandled Exception: SQLite.SQLiteException: A dynamic link library (DLL) initialization routine failed.
Debugging
Testing SQLite wrapper
My first approach was to replace System.Data.SQLite Version 1.0.99 SQLite 3.9.2. Since it has a custom interop assembly (SQLite.Interop.dll), I thought it could be something regarding the way it was compiled.
I’ve then tried to load mod_spatialite using sqlite-net. No lucky. I’ve even used libsqlite3-0 shipped with mod_spatialite-4.3.0a-win-amd64 to be sure to be running correct versions. I’ve got the same error.
Testing mod_spatialite
Since I got two different paths leading to the very same problem, I guessed it was something to do with mod_spatialite it self. I’ve download mod_spatialite-4.3.0a-win-amd64 and tested it using the bundled sqlite3.exe.
It was able to load mod_spatialite without any problems.
Digging depper with WinDBG
Since everything was working perfecly when separated, I’ve decided to dig deeper with WinDBG. Then, I got an Access violation - code c0000005 after issuing trying to load mod_spatialite with the command below.
1
SELECT load_extension('mod_spatialite')
Below is call stack when the error occurred. Looks like something went wrong during libstdc++_64-6.dll.
Well, libstdc++_64-6.dll has nothing to do with spatialite it self, it’s just a dependency. To be sure it was something with lib initialization I’ve tried to load it manually with the code below. As expected it failed.
var intPtr = LoadLibrary("libstdc++_64-6.dll"); } }
Digging into libstdc++-6.dll
libstdc++-6.dll is part of MinGW-W64. Since the DLL hasn’t any information regarding it’s version I’ve then download the latest MinGW-W64 x86_64-5.3.0-win32-seh-rt_v4-rev0. The downloaded version of libstdc++-6.dll loaded perfectly fine using LoadLibrary("libstdc++-6.dll");
Updating libstdc++-6.dll
libstdc++-6.dll has a dependency to libgcc_s_seh-1.dll. So, both DLLs should be updated. Since mod_spatialite.dll is looking from libstdc++_64-6.dll instead of libstdc++-6.dll you have to rename it to the correct name. As the new libstdc++-6.dll is looking for libgcc_s_seh-1.dll instead of libgcc_s_seh_64-1.dll, you can delete libgcc_s_seh_64-1.dll shipped mod_spatialite-4.3.0a-win-amd64 and just copy libgcc_s_seh-1.dll.
Conclusion
After upgrading libstdc++-6.dll everything worked as expected. I’ve tested it against the following scenario:
Windows 10 x64 - [Version 10.0.10586]
.NET Framework v 4.6.1
System.Data.SQLite Version 1.0.99 [SQLite 3.9.2]
mod_spatialite-4.3.0a-win-amd64.
I’ve also posted to spatialite-users mailist this workaround. As Sandro Furieri pointed out, it may be not safe to just replace core DLLs in this way because the ABI might have changed. I’ve been using this workaround without any issues so far.
I hope this module gets compiled with a newer version of MinGW-W64 in the next release.