diff options
author | nulltoken <emeric.fermas@gmail.com> | 2014-05-22 23:27:11 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2014-06-07 20:00:15 +0400 |
commit | ed3eda06c1ec31b52a3b85d5231ed8ebc72cc243 (patch) | |
tree | d91211fa3421a7464b0b65c18763869b71ca2fb3 /LibGit2Sharp | |
parent | 5dcf507e1d46cba72b2b329b20b050e4f5da3e72 (diff) |
Decorrelate IDisposable implementation from freeing of OdbBackend resources
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/ObjectDatabase.cs | 4 | ||||
-rw-r--r-- | LibGit2Sharp/OdbBackend.cs | 27 |
2 files changed, 25 insertions, 6 deletions
diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index 2a51fad5..d7752b41 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -99,6 +99,10 @@ namespace LibGit2Sharp /// <summary> /// Adds the provided backend to the object database with the specified priority. + /// <para> + /// If the provided backend implements <see cref="IDisposable"/>, the <see cref="IDisposable.Dispose"/> + /// method will be honored and invoked upon the disposal of the repository. + /// </para> /// </summary> /// <param name="backend">The backend to add</param> /// <param name="priority">The priority at which libgit2 should consult this backend (higher values are consulted first)</param> diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index b174ce78..149b996c 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -8,20 +8,26 @@ namespace LibGit2Sharp { /// <summary> /// Base class for all custom managed backends for the libgit2 object database (ODB). + /// <para> + /// If the derived backend implements <see cref="IDisposable"/>, the <see cref="IDisposable.Dispose"/> + /// method will be honored and invoked upon the disposal of the repository. + /// </para> /// </summary> public abstract class OdbBackend { /// <summary> /// Invoked by libgit2 when this backend is no longer needed. /// </summary> - protected virtual void Dispose() + internal void Free() { - if (IntPtr.Zero != nativeBackendPointer) + if (nativeBackendPointer == IntPtr.Zero) { - GCHandle.FromIntPtr(Marshal.ReadIntPtr(nativeBackendPointer, GitOdbBackend.GCHandleOffset)).Free(); - Marshal.FreeHGlobal(nativeBackendPointer); - nativeBackendPointer = IntPtr.Zero; + return; } + + GCHandle.FromIntPtr(Marshal.ReadIntPtr(nativeBackendPointer, GitOdbBackend.GCHandleOffset)).Free(); + Marshal.FreeHGlobal(nativeBackendPointer); + nativeBackendPointer = IntPtr.Zero; } /// <summary> @@ -588,7 +594,16 @@ namespace LibGit2Sharp try { - odbBackend.Dispose(); + odbBackend.Free(); + + var disposable = odbBackend as IDisposable; + + if (disposable == null) + { + return; + } + + disposable.Dispose(); } catch (Exception ex) { |