Index: System.Data.SQLite/SQLite3.cs ================================================================== --- System.Data.SQLite/SQLite3.cs +++ System.Data.SQLite/SQLite3.cs @@ -536,11 +536,11 @@ if (n > 0) throw new SQLiteException(n, GetLastError()); strRemain = UTF8ToString(ptr, len); - if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, new SQLiteStatementHandle(stmt), strSql.Substring(0, strSql.Length - strRemain.Length), previous); + if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, new SQLiteStatementHandle(_sql, stmt), strSql.Substring(0, strSql.Length - strRemain.Length), previous); return cmd; } finally { @@ -1478,12 +1478,12 @@ if (backup == IntPtr.Zero) throw new SQLiteException(ResultCode(), GetLastError()); return new SQLiteBackup( - this, new SQLiteBackupHandle(backup), destHandle, zDestName, - sourceHandle, zSourceName); + this, new SQLiteBackupHandle(destHandle, backup), + destHandle, zDestName, sourceHandle, zSourceName); } /// /// Copies up to N pages from the source database to the destination /// database associated with the specified backup object. Index: System.Data.SQLite/SQLiteBase.cs ================================================================== --- System.Data.SQLite/SQLiteBase.cs +++ System.Data.SQLite/SQLiteBase.cs @@ -365,26 +365,32 @@ #pragma warning disable 162 GC.KeepAlive(hdl); /* NOTE: Unreachable code. */ #pragma warning restore 162 } - internal static void FinishBackup(IntPtr backup) - { - if (backup == IntPtr.Zero) return; - int n = UnsafeNativeMethods.sqlite3_backup_finish(backup); - if (n > 0) throw new SQLiteException(n, null); - } - - internal static void FinalizeStatement(IntPtr stmt) - { - if (stmt == IntPtr.Zero) return; -#if !SQLITE_STANDARD - int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt); -#else - int n = UnsafeNativeMethods.sqlite3_finalize(stmt); -#endif - if (n > 0) throw new SQLiteException(n, null); + internal static void FinishBackup(SQLiteConnectionHandle hdl, IntPtr backup) + { + if ((hdl == null) || (backup == IntPtr.Zero)) return; + lock (hdl) + { + int n = UnsafeNativeMethods.sqlite3_backup_finish(backup); + if (n > 0) throw new SQLiteException(n, null); + } + } + + internal static void FinalizeStatement(SQLiteConnectionHandle hdl, IntPtr stmt) + { + if ((hdl == null) || (stmt == IntPtr.Zero)) return; + lock (hdl) + { +#if !SQLITE_STANDARD + int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt); +#else + int n = UnsafeNativeMethods.sqlite3_finalize(stmt); +#endif + if (n > 0) throw new SQLiteException(n, null); + } } internal static void CloseConnection(SQLiteConnectionHandle hdl, IntPtr db) { if ((hdl == null) || (db == IntPtr.Zero)) return; Index: System.Data.SQLite/UnsafeNativeMethods.cs ================================================================== --- System.Data.SQLite/UnsafeNativeMethods.cs +++ System.Data.SQLite/UnsafeNativeMethods.cs @@ -1455,18 +1455,21 @@ } // Provides finalization support for unmanaged SQLite statements. internal class SQLiteStatementHandle : CriticalHandle { + private SQLiteConnectionHandle cnn; + public static implicit operator IntPtr(SQLiteStatementHandle stmt) { return (stmt != null) ? stmt.handle : IntPtr.Zero; } - internal SQLiteStatementHandle(IntPtr stmt) + internal SQLiteStatementHandle(SQLiteConnectionHandle cnn, IntPtr stmt) : this() { + this.cnn = cnn; SetHandle(stmt); } private SQLiteStatementHandle() : base(IntPtr.Zero) @@ -1480,11 +1483,11 @@ #if !PLATFORM_COMPACTFRAMEWORK IntPtr localHandle = Interlocked.Exchange( ref handle, IntPtr.Zero); if (localHandle != IntPtr.Zero) - SQLiteBase.FinalizeStatement(localHandle); + SQLiteBase.FinalizeStatement(cnn, localHandle); #if DEBUG && !NET_COMPACT_20 try { Trace.WriteLine(String.Format( @@ -1495,11 +1498,11 @@ } #endif #else if (handle != IntPtr.Zero) { - SQLiteBase.FinalizeStatement(handle); + SQLiteBase.FinalizeStatement(cnn, handle); SetHandle(IntPtr.Zero); } #endif #if DEBUG @@ -1549,18 +1552,21 @@ } // Provides finalization support for unmanaged SQLite backup objects. internal class SQLiteBackupHandle : CriticalHandle { + private SQLiteConnectionHandle cnn; + public static implicit operator IntPtr(SQLiteBackupHandle backup) { return (backup != null) ? backup.handle : IntPtr.Zero; } - internal SQLiteBackupHandle(IntPtr backup) + internal SQLiteBackupHandle(SQLiteConnectionHandle cnn, IntPtr backup) : this() { + this.cnn = cnn; SetHandle(backup); } private SQLiteBackupHandle() : base(IntPtr.Zero) @@ -1574,11 +1580,11 @@ #if !PLATFORM_COMPACTFRAMEWORK IntPtr localHandle = Interlocked.Exchange( ref handle, IntPtr.Zero); if (localHandle != IntPtr.Zero) - SQLiteBase.FinishBackup(localHandle); + SQLiteBase.FinishBackup(cnn, localHandle); #if DEBUG && !NET_COMPACT_20 try { Trace.WriteLine(String.Format( @@ -1589,11 +1595,11 @@ } #endif #else if (handle != IntPtr.Zero) { - SQLiteBase.FinishBackup(handle); + SQLiteBase.FinishBackup(cnn, handle); SetHandle(IntPtr.Zero); } #endif #if DEBUG