1

I am trying to use Spatialite in a .NET application using the System.Data.SQLite provider.

I have tried to load 'libspatialite-4.dll' but an error happens when I call function "conn.EnableExtensions(true);conn.LoadExtension("libspatialite-4.dll");"

The specific error is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I am using sqlite-netFx40-setup-x64-2010-1.0.92.0,libspatialite-4.1.1, VS2010 SP1, .net Framework 4.0, Windows7 64bit,

8
  • To piggy-back my answer if you can provide more code I am currently working on a .net project using System.Data.SQLite as well and may be able to track down the specific problem for you. – GeoSharp May 9 '14 at 9:31
  • SQLiteConnection conn=new SQLiteConnection("Data Source=E://GuDongElectric/zhdb(1220).sqlite;Pooling=true;FailIfMissing=false");conn.Open();conn.EnableExtensions(true); conn.LoadExtension("libspatialite-4.dll"); when run into the last line ,got the issue. – Simon He May 9 '14 at 14:01
  • I am not familiar with this LoadExtension() method but in your sample code you are only providing a filename. Are you shipping this .dll with the .net project? Make sure your code knows where this relative path to the .dll is – GeoSharp May 9 '14 at 14:52
  • I put all of dlls(sqlite,spatialite) to the same path with my project .exe file – Simon He May 9 '14 at 15:02
  • Whether the version of sqlite and spatialite do not match? I installed sqlite using sqlite-netFx40-setup-x64-2010-1.0.92.0.exe , and use libspatialite-4.1.1 – Simon He May 9 '14 at 15:13
2

This is an exception/error that often occurs when you are dealing with unmanaged code i.e. calls to COM Objects or C/C++ .dll files in your .NET code. It is a difficult bug to track down because the error is occurring within the unmanaged code or when you are passing arguments to the unmanaged code. The message:

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

is a general exception message and is not always very telling to what the problem really is. Refer to this link, specifically the answer given by Shanks Zen for more info on the topic.

It appears you are not using the correct syntax for calling the .dll which is why you are getting the exception that you are. Here is a code sample from github of how to correctly load spatialite:

using System.Data.SQLite;

using (DbConnection connection = new SQLiteConnection("Data Source=" + database)) {
connection.Open(); // load the extension 
using (DbCommand command = connection.CreateCommand()) {
    //Load the libspatialite library extension - *.dll on windows, *.a on iOS
    command.CommandText = "SELECT load_extension('libspatialite-2.dll');";
    command.ExecuteNonQuery(); // Run queries here
    }
}
1
  • I try setting Visual Studio Debugger to bypass this exception:Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load",but the issue still exist – Simon He May 9 '14 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.