From d6486be3416258f46a791fe0f6344044a3929601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ag=C3=BC=C3=AD?= Date: Mon, 6 Apr 2015 17:25:39 +0200 Subject: [PATCH] Levarages sqlite_close_2 on the finalizer. Acknownledges the `disposing` flag passed either by the `Dispose` or the `~SQLiteConnection` finalizer. Prevents throwing exceptions on the finalizer thread. Prevents calling dispose on the _mappings' sqlInsertCommand when called via finalizer --- src/SQLite.cs | 88 +++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index ea5386fe4..734b2de5b 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -1483,42 +1483,48 @@ public int DeleteAll () return count; } - ~SQLiteConnection () - { - Dispose (false); - } - - public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) - { - Close (); - } - - public void Close () - { - if (_open && Handle != NullHandle) { - try { - if (_mappings != null) { - foreach (var sqlInsertCommand in _mappings.Values) { - sqlInsertCommand.Dispose(); - } - } - var r = SQLite3.Close (Handle); - if (r != SQLite3.Result.OK) { - string msg = SQLite3.GetErrmsg (Handle); - throw SQLiteException.New (r, msg); - } - } - finally { - Handle = NullHandle; - _open = false; - } - } + ~SQLiteConnection () + { + Dispose (false); + } + + public void Dispose () + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public void Close() + { + Dispose(true); + } + + protected virtual void Dispose(bool disposing) + { + if (_open && Handle != NullHandle) { + try { + if (disposing) { + if (_mappings != null) { + foreach (var sqlInsertCommand in _mappings.Values){ + sqlInsertCommand.Dispose(); + } + } + + var r = SQLite3.Close(Handle); + if (r != SQLite3.Result.OK) + { + string msg = SQLite3.GetErrmsg(Handle); + throw SQLiteException.New(r, msg); + } + } else { + SQLite3.Close2(Handle); + } + } + finally { + Handle = NullHandle; + _open = false; + } + } } void OnTableChanged (TableMapping table, NotifyTableChangedAction action) @@ -2999,7 +3005,10 @@ public enum ConfigOption : int public static extern Result EnableLoadExtension (IntPtr db, int onoff); [DllImport("sqlite3", EntryPoint = "sqlite3_close", CallingConvention=CallingConvention.Cdecl)] - public static extern Result Close (IntPtr db); + public static extern Result Close (IntPtr db); + + [DllImport("sqlite3", EntryPoint = "sqlite3_close_v2", CallingConvention = CallingConvention.Cdecl)] + public static extern Result Close2(IntPtr db); [DllImport("sqlite3", EntryPoint = "sqlite3_initialize", CallingConvention=CallingConvention.Cdecl)] public static extern Result Initialize(); @@ -3159,6 +3168,11 @@ public static Result Close(Sqlite3DatabaseHandle db) return (Result)Sqlite3.sqlite3_close(db); } + public static Result Close2(Sqlite3DatabaseHandle db) + { + return (Result)Sqlite3.sqlite3_close_v2(db); + } + public static Result BusyTimeout(Sqlite3DatabaseHandle db, int milliseconds) { return (Result)Sqlite3.sqlite3_busy_timeout(db, milliseconds);