I am trying to use Spatialite in a .NET application using the System.Data.SQLite provider. Interestingly enough, everything works as expected except an error gets thrown at the very end when the connection is closed. The specific error is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." The same thing happens if I wrap this function is a using statement. If I remove the conn.Close() it still bombs out.

Any ideas?

                conn = new SQLiteConnection(connectionString);
                cmd = conn.CreateCommand();

                cmd.CommandText = @"SELECT load_extension('libspatialite-2.dll');";

                cmd.CommandText = "SELECT AsBinary(Geometry) AS WKB from country";
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                    //do work

                // error handling here
                if (!rdr.IsClosed)

                rdr = null;
                cmd = null;
                conn = null;

ask your question on http://groups.google.com/group/spatialite-users/topics?pli=1

There are many references on the use of .NET


Just came here looking for my problem. I was able to figure out the reason for the exception on closing connection.

I was using SQLite dll from: http://sqlite.phxsoftware.com/ (this project is not maintained by the author any more, last time it was updated in April 2010), the dll System.Data.Sqlite was of older version. Found a new version of System.Data.Sqlite on : http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki  - Downloaded the .Net framework 4 binaries and added Two files :  System.Data.Sqlite.dll and System.Interop.dll  Since then I am not getting any exception on application close. 


Even if it doesn't directly cause the exception you're getting, your code is faulty because you handle everything within a single try-catch block instead of relying on usings. What happens if an exception occurs before rdr = cmd.ExecuteReader();? Then rdr will be null and the code if (!rdr.IsClosed) will throw another exception inside the catch block.

Instead of complicating things with try-catch cleanup code you should (almost) always use usings - and use them for all disposable objects (in your case conn, cmd and rdr). This way the scope of the variable will be clearly visible and you'll also know that the cleanup code will always run the way it is supposed to.

  • I had initially wrapped all of this in using statements - as I stated in my original post and still had the same problem. Although I appreciate the lecture on code semantics, your comment don't really address the root problem. – user890 Oct 21 '11 at 17:38
  • 1
    My point was that before investigating such issues (and asking questions on forums), it is a good idea to make sure the code is clean. And your bug in the code still stands, so I don't see why it is me that deserves -1. – Igor Brejc Oct 21 '11 at 18:50

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.