This repository was archived by the owner on Feb 3, 2023. It is now read-only.
forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSafeHandleBase.cs
More file actions
207 lines (183 loc) · 6.52 KB
/
Copy pathSafeHandleBase.cs
File metadata and controls
207 lines (183 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// This activates a lightweight mode which will help put under the light
// incorrectly released handles by outputing a warning message in the console.
//
// This should be activated when tests are being run of the CI server.
//
// Uncomment the line below or add a conditional symbol to activate this mode
//#define LEAKS_IDENTIFYING
// This activates a more throrough mode which will show the stack trace of the
// allocation code path for each handle that has been improperly released.
//
// This should be manually activated when some warnings have been raised as
// a result of LEAKS_IDENTIFYING mode activation.
//
// Uncomment the line below or add a conditional symbol to activate this mode
//#define LEAKS_TRACKING
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Threading;
#if LEAKS_IDENTIFYING
namespace LibGit2Sharp.Core
{
/// <summary>
/// Holds leaked handle type names reported by <see cref="Core.Handles.SafeHandleBase"/>
/// </summary>
public static class LeaksContainer
{
private static readonly HashSet<string> _typeNames = new HashSet<string>();
private static readonly object _lockpad = new object();
/// <summary>
/// Report a new leaked handle type name
/// </summary>
/// <param name="typeName">Short name of the leaked handle type.</param>
public static void Add(string typeName)
{
lock (_lockpad)
{
_typeNames.Add(typeName);
}
}
/// <summary>
/// Removes all previously reported leaks.
/// </summary>
public static void Clear()
{
lock (_lockpad)
{
_typeNames.Clear();
}
}
/// <summary>
/// Returns all reported leaked handle type names.
/// </summary>
public static IEnumerable<string> TypeNames
{
get
{
string[] result = null;
lock (_lockpad)
{
result = _typeNames.ToArray();
}
return result;
}
}
}
}
#endif
namespace LibGit2Sharp.Core.Handles
{
internal abstract class SafeHandleBase : SafeHandle
{
#if LEAKS_TRACKING
private readonly string trace;
private readonly Guid id;
#endif
/// <summary>
/// This is set to non-zero when <see cref="NativeMethods.AddHandle"/> has
/// been called for this handle, but <see cref="NativeMethods.RemoveHandle"/>
/// has not yet been called.
/// </summary>
private int registered;
protected SafeHandleBase()
: base(IntPtr.Zero, true)
{
NativeMethods.AddHandle();
registered = 1;
#if LEAKS_TRACKING
id = Guid.NewGuid();
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Allocating {0} handle ({1})", GetType().Name, id));
trace = new StackTrace(2, true).ToString();
#endif
}
protected override void Dispose(bool disposing)
{
bool leaked = !disposing && !IsInvalid;
#if LEAKS_IDENTIFYING
if (leaked)
{
LeaksContainer.Add(GetType().Name);
}
#endif
base.Dispose(disposing);
#if LEAKS_TRACKING
if (!leaked)
{
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Disposing {0} handle ({1})", GetType().Name, id));
}
else
{
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unexpected finalization of {0} handle ({1})", GetType().Name, id));
Trace.WriteLine(trace);
Trace.WriteLine("");
}
#endif
}
// Prevent the debugger from evaluating this property because it has side effects
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public override sealed bool IsInvalid
{
get
{
bool invalid = IsInvalidImpl();
if (invalid && Interlocked.CompareExchange(ref registered, 0, 1) == 1)
{
/* Unregister the handle because we know ReleaseHandle won't be called
* to do it for us.
*/
NativeMethods.RemoveHandle();
}
return invalid;
}
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected virtual bool IsInvalidImpl()
{
return handle == IntPtr.Zero;
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected abstract bool ReleaseHandleImpl();
protected override sealed bool ReleaseHandle()
{
bool result;
try
{
result = ReleaseHandleImpl();
}
finally
{
if (Interlocked.CompareExchange(ref registered, 0, 1) == 1)
{
// if the handle is still registered at this point, we definitely
// want to unregister it
NativeMethods.RemoveHandle();
}
else
{
/* For this to be called, the following sequence of events must occur:
*
* 1. The handle is created
* 2. The IsInvalid property is evaluated, and the result is false
* 3. The IsInvalid property is evaluated by the runtime to determine if
* finalization is necessary, and the result is now true
*
* This can only happen if the value of `handle` is manipulated in an unexpected
* way (through the Reflection API or by a specially-crafted derived type that
* does not currently exist). The only safe course of action at this point in
* the shutdown process is returning false, which will trigger the
* releaseHandleFailed MDA but have no other impact on the CLR state.
* http://msdn.microsoft.com/en-us/library/85eak4a0.aspx
*/
result = false;
}
}
return result;
}
}
}