1
1
// This activates a lightweight mode which will help put under the light
2
- // incorrectly released handles by outputing a warning message in the console.
2
+ // incorrectly released handles by outputting a warning message in the console.
3
3
//
4
4
// This should be activated when tests are being run on the CI server.
5
5
//
6
6
// Uncomment the line below or add a conditional symbol to activate this mode
7
7
8
- // #define LEAKS_IDENTIFYING
8
+ //#define LEAKS_IDENTIFYING
9
9
10
- // This activates a more throrough mode which will show the stack trace of the
10
+ // This activates a more thorough mode which will show the stack trace of the
11
11
// allocation code path for each handle that has been improperly released.
12
12
//
13
13
// This should be manually activated when some warnings have been raised as
14
14
// a result of LEAKS_IDENTIFYING mode activation.
15
15
//
16
16
// Uncomment the line below or add a conditional symbol to activate this mode
17
17
18
- // #define LEAKS_TRACKING
18
+ //#define LEAKS_TRACKING
19
19
20
20
using System ;
21
- using System . Linq ;
22
- using System . Diagnostics ;
23
- using System . Globalization ;
24
- using System . Collections . Generic ;
21
+ using Microsoft . Win32 . SafeHandles ;
25
22
26
23
#if LEAKS_IDENTIFYING
27
24
namespace LibGit2Sharp . Core
28
25
{
26
+ using System . Collections . Generic ;
27
+ using System . Linq ;
28
+
29
29
/// <summary>
30
30
/// Holds leaked handle type names reported by <see cref="Core.Handles.Libgit2Object"/>
31
31
/// </summary>
@@ -78,30 +78,27 @@ public static IEnumerable<string> TypeNames
78
78
79
79
namespace LibGit2Sharp . Core . Handles
80
80
{
81
- internal unsafe abstract class Libgit2Object : IDisposable
81
+ #if LEAKS_TRACKING
82
+ using System . Diagnostics ;
83
+ using System . Globalization ;
84
+ #endif
85
+
86
+ internal unsafe abstract class Libgit2Object : SafeHandleZeroOrMinusOneIsInvalid
82
87
{
83
88
#if LEAKS_TRACKING
84
89
private readonly string trace ;
85
90
private readonly Guid id ;
86
91
#endif
87
92
88
- protected void * ptr ;
89
-
90
- internal void * Handle
93
+ internal unsafe Libgit2Object ( void * ptr , bool owned )
94
+ : this ( new IntPtr ( ptr ) , owned )
91
95
{
92
- get
93
- {
94
- return ptr ;
95
- }
96
96
}
97
97
98
- bool owned ;
99
- bool disposed ;
100
-
101
- internal unsafe Libgit2Object ( void * handle , bool owned )
98
+ internal unsafe Libgit2Object ( IntPtr ptr , bool owned )
99
+ : base ( owned )
102
100
{
103
- this . ptr = handle ;
104
- this . owned = owned ;
101
+ SetHandle ( ptr ) ;
105
102
106
103
#if LEAKS_TRACKING
107
104
id = Guid . NewGuid ( ) ;
@@ -111,53 +108,20 @@ internal unsafe Libgit2Object(void* handle, bool owned)
111
108
#endif
112
109
}
113
110
114
- internal unsafe Libgit2Object ( IntPtr ptr , bool owned )
115
- : this ( ptr . ToPointer ( ) , owned )
116
- {
117
- }
111
+ internal IntPtr AsIntPtr ( ) => DangerousGetHandle ( ) ;
118
112
119
- ~ Libgit2Object ( )
120
- {
121
- Dispose ( false ) ;
122
- }
123
-
124
- internal bool IsNull
125
- {
126
- get
127
- {
128
- return ptr == null ;
129
- }
130
- }
131
-
132
- internal IntPtr AsIntPtr ( )
133
- {
134
- return new IntPtr ( ptr ) ;
135
- }
136
-
137
- public abstract void Free ( ) ;
138
-
139
- void Dispose ( bool disposing )
113
+ protected override void Dispose ( bool disposing )
140
114
{
141
115
#if LEAKS_IDENTIFYING
142
- bool leaked = ! disposing && ptr != null ;
116
+ bool leaked = ! disposing && DangerousGetHandle ( ) != IntPtr . Zero ;
143
117
144
118
if ( leaked )
145
119
{
146
120
LeaksContainer . Add ( GetType ( ) . Name ) ;
147
121
}
148
122
#endif
149
123
150
- if ( ! disposed )
151
- {
152
- if ( owned )
153
- {
154
- Free ( ) ;
155
- }
156
-
157
- ptr = null ;
158
- }
159
-
160
- disposed = true ;
124
+ base . Dispose ( disposing ) ;
161
125
162
126
#if LEAKS_TRACKING
163
127
if ( ! leaked )
@@ -172,11 +136,6 @@ void Dispose(bool disposing)
172
136
}
173
137
#endif
174
138
}
175
-
176
- public void Dispose ( )
177
- {
178
- Dispose ( true ) ;
179
- }
180
139
}
181
140
}
182
141
0 commit comments