Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

try
            {
                conn = new SQLiteConnection(connectionString);
                conn.Open();
                cmd = conn.CreateCommand();

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

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

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

            }
            catch
            {
                // error handling here
            }
            finally
            {
                if (!rdr.IsClosed)
                    rdr.Close();

                rdr = null;
                cmd = null;
                conn.Close();
                conn = null;
            }
share|improve this question

3 Answers 3

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

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

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
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

 
discard

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.