diff --git a/LibGit2Sharp/Core/EncodingMarshaler.cs b/LibGit2Sharp/Core/EncodingMarshaler.cs index 08907ce1..8a498cc7 100644 --- a/LibGit2Sharp/Core/EncodingMarshaler.cs +++ b/LibGit2Sharp/Core/EncodingMarshaler.cs @@ -21,12 +21,7 @@ namespace LibGit2Sharp.Core public virtual void CleanUpNativeData(IntPtr pNativeData) { - if (pNativeData == IntPtr.Zero) - { - return; - } - - Marshal.FreeHGlobal(pNativeData); + Cleanup(pNativeData); } public int GetNativeDataSize() @@ -83,6 +78,16 @@ namespace LibGit2Sharp.Core return new IntPtr(buffer); } + public static void Cleanup(IntPtr pNativeData) + { + if (pNativeData == IntPtr.Zero) + { + return; + } + + Marshal.FreeHGlobal(pNativeData); + } + public static unsafe String FromNative(Encoding encoding, IntPtr pNativeData) { if (pNativeData == IntPtr.Zero) diff --git a/LibGit2Sharp/Core/GitRepositoryInitOptions.cs b/LibGit2Sharp/Core/GitRepositoryInitOptions.cs index da202b94..039b6c9d 100644 --- a/LibGit2Sharp/Core/GitRepositoryInitOptions.cs +++ b/LibGit2Sharp/Core/GitRepositoryInitOptions.cs @@ -41,12 +41,7 @@ namespace LibGit2Sharp.Core public void Dispose() { - if (WorkDirPath == IntPtr.Zero) - { - return; - } - - Marshal.FreeHGlobal(WorkDirPath); + EncodingMarshaler.Cleanup(WorkDirPath); WorkDirPath = IntPtr.Zero; } } diff --git a/LibGit2Sharp/Core/GitStrArrayIn.cs b/LibGit2Sharp/Core/GitStrArrayIn.cs index 6ec4256c..14565ab9 100644 --- a/LibGit2Sharp/Core/GitStrArrayIn.cs +++ b/LibGit2Sharp/Core/GitStrArrayIn.cs @@ -45,7 +45,7 @@ namespace LibGit2Sharp.Core for (int i = 0; i < nbOfPaths; i++) { - Marshal.FreeHGlobal(pathPtrs[i]); + EncodingMarshaler.Cleanup(pathPtrs[i]); } Marshal.FreeHGlobal(strings); diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index dde8b28a..6856f580 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -5,7 +5,6 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; using LibGit2Sharp.Core.Handles; @@ -554,7 +553,7 @@ namespace LibGit2Sharp }; Proxy.git_index_add(handle, indexEntry); - Marshal.FreeHGlobal(indexEntry.Path); + EncodingMarshaler.Cleanup(indexEntry.Path); } internal void ReloadFromDisk()