Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
share|improve this question

ask your question on

There are many references on the use of .NET

share|improve this answer

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: (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 :  - 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. 

share|improve this answer

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.

share|improve this answer
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
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 posting your answer, you agree to the privacy policy and terms of service.

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