Skip to content

Commit d2d5b41

Browse files
committed
Revert "Remove handle when dispose is called for the first time"
This reverts commit 05d23bb.
1 parent 05d23bb commit d2d5b41

17 files changed

+32
-25
lines changed

LibGit2Sharp/Core/Handles/ConfigurationSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class ConfigurationSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_config_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/DiffListSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace LibGit2Sharp.Core.Handles
22
{
33
internal class DiffListSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_diff_list_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/GitObjectSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class GitObjectSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_object_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/IndexSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class IndexSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_index_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/NoteSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class NoteSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_note_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/NullGitObjectSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public NullGitObjectSafeHandle()
99
handle = IntPtr.Zero;
1010
}
1111

12-
protected override bool ReleaseHandle()
12+
protected override bool ReleaseHandleImpl()
1313
{
1414
// Nothing to release
1515
return true;

LibGit2Sharp/Core/Handles/NullIndexSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public NullIndexSafeHandle()
99
handle = IntPtr.Zero;
1010
}
1111

12-
protected override bool ReleaseHandle()
12+
protected override bool ReleaseHandleImpl()
1313
{
1414
// Nothing to release
1515
return true;

LibGit2Sharp/Core/Handles/ObjectDatabaseSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class ObjectDatabaseSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_odb_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/PushSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class PushSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_push_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/ReferenceSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class ReferenceSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_reference_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/RemoteSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class RemoteSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_remote_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/RepositorySafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class RepositorySafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_repository_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/RevWalkerSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class RevWalkerSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_revwalk_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/SafeHandleBase.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.Globalization;
44
using System.Runtime.InteropServices;
5-
using System.Threading;
65

76
namespace LibGit2Sharp.Core.Handles
87
{
@@ -12,8 +11,6 @@ internal abstract class SafeHandleBase : SafeHandle
1211
private readonly string trace;
1312
#endif
1413

15-
private int disposeCount = 0;
16-
1714
protected SafeHandleBase()
1815
: base(IntPtr.Zero, true)
1916
{
@@ -23,9 +20,9 @@ protected SafeHandleBase()
2320
#endif
2421
}
2522

23+
#if DEBUG
2624
protected override void Dispose(bool disposing)
2725
{
28-
#if DEBUG
2926
if (!disposing && !IsInvalid)
3027
{
3128
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "A {0} handle wrapper has not been properly disposed.", GetType().Name));
@@ -34,18 +31,28 @@ protected override void Dispose(bool disposing)
3431
#endif
3532
Trace.WriteLine("");
3633
}
37-
#endif
38-
base.Dispose(disposing);
3934

40-
if (Interlocked.Increment(ref disposeCount) == 1)
41-
NativeMethods.RemoveHandle();
35+
base.Dispose(disposing);
4236
}
37+
#endif
4338

4439
public override bool IsInvalid
4540
{
4641
get { return (handle == IntPtr.Zero); }
4742
}
4843

49-
protected abstract override bool ReleaseHandle();
44+
protected abstract bool ReleaseHandleImpl();
45+
46+
protected override sealed bool ReleaseHandle()
47+
{
48+
try
49+
{
50+
return ReleaseHandleImpl();
51+
}
52+
finally
53+
{
54+
NativeMethods.RemoveHandle();
55+
}
56+
}
5057
}
5158
}

LibGit2Sharp/Core/Handles/SignatureSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class SignatureSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_signature_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/TreeBuilderSafeHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace LibGit2Sharp.Core.Handles
22
{
33
internal class TreeBuilderSafeHandle : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_treebuilder_free(handle);
88
return true;

LibGit2Sharp/Core/Handles/TreeEntrySafeHandle_Owned.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
internal class TreeEntrySafeHandle_Owned : SafeHandleBase
44
{
5-
protected override bool ReleaseHandle()
5+
protected override bool ReleaseHandleImpl()
66
{
77
Proxy.git_tree_entry_free(handle);
88
return true;

0 commit comments

Comments
 (0)