forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibgit2Object.cs
More file actions
182 lines (154 loc) · 4.42 KB
/
Libgit2Object.cs
File metadata and controls
182 lines (154 loc) · 4.42 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
// 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 on 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.Linq;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
#if LEAKS_IDENTIFYING
namespace LibGit2Sharp.Core
{
/// <summary>
/// Holds leaked handle type names reported by <see cref="Core.Handles.Libgit2Object"/>
/// </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 unsafe abstract class Libgit2Object : IDisposable
{
#if LEAKS_TRACKING
private readonly string trace;
private readonly Guid id;
#endif
protected void* ptr;
internal void* Handle
{
get
{
return ptr;
}
}
bool owned;
bool disposed;
internal unsafe Libgit2Object(void* handle, bool owned)
{
this.ptr = handle;
this.owned = owned;
#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
}
internal unsafe Libgit2Object(IntPtr ptr, bool owned)
: this(ptr.ToPointer(), owned)
{
}
~Libgit2Object()
{
Dispose(false);
}
internal bool IsNull
{
get
{
return ptr == null;
}
}
internal IntPtr AsIntPtr()
{
return new IntPtr(ptr);
}
public abstract void Free();
void Dispose(bool disposing)
{
#if LEAKS_IDENTIFYING
bool leaked = !disposing && ptr != null;
if (leaked)
{
LeaksContainer.Add(GetType().Name);
}
#endif
if (!disposed)
{
if (owned)
{
Free();
}
ptr = null;
}
disposed = true;
#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
}
public void Dispose()
{
Dispose(true);
}
}
}