This repository was archived by the owner on Jun 14, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +28
-21
lines changed
src/Microsoft.Management.Infrastructure Expand file tree Collapse file tree 4 files changed +28
-21
lines changed Original file line number Diff line number Diff line change @@ -384,7 +384,10 @@ private void Dispose(bool disposing)
384384 {
385385 if ( this . InstanceHandle != null && ! this . InstanceHandle . IsNull )
386386 {
387- this . InstanceHandle . Delete ( ) ;
387+ using ( this . InstanceHandle )
388+ {
389+ this . InstanceHandle . Delete ( ) ;
390+ }
388391 }
389392 }
390393 }
Original file line number Diff line number Diff line change 33
44namespace Microsoft . Management . Infrastructure . Native
55{
6- internal class MI_Instance : MI_NativeObjectWithFT < MI_Instance . MI_InstanceFT >
6+ internal class MI_Instance : MI_NativeObjectWithFT < MI_Instance . MI_InstanceFT > , IDisposable
77 {
88 internal MI_Result GetElement (
99 string name ,
@@ -124,21 +124,6 @@ internal MI_Result Delete()
124124 return localResult ;
125125 }
126126
127- private void ZeroPtr ( )
128- {
129- if ( this . isDirect )
130- {
131- this . allocatedData = IntPtr . Zero ;
132- }
133- else if ( this . allocatedData != IntPtr . Zero )
134- {
135- unsafe
136- {
137- * ( IntPtr * ) this . allocatedData = IntPtr . Zero ;
138- }
139- }
140- }
141-
142127 internal MI_Result IsA (
143128 MI_ClassDecl classDecl ,
144129 out bool flag
Original file line number Diff line number Diff line change 33
44namespace Microsoft . Management . Infrastructure . Native
55{
6- internal abstract class MI_NativeObject
6+ internal abstract class MI_NativeObject : IDisposable
77 {
88 [ StructLayout ( LayoutKind . Sequential , CharSet = MI_PlatformSpecific . AppropriateCharSet ) ]
99 protected struct MI_NativeObjectNormalMembersLayout
@@ -60,11 +60,13 @@ internal struct ArrayPtr
6060 protected IntPtr allocatedData ;
6161 protected bool isDirect ;
6262
63- ~ MI_NativeObject ( )
63+ public virtual void Dispose ( )
6464 {
6565 if ( this . allocatedData != IntPtr . Zero )
6666 {
67- Marshal . FreeHGlobal ( this . allocatedData ) ;
67+ IntPtr tmp = this . allocatedData ;
68+ this . ZeroPtr ( ) ;
69+ Marshal . FreeHGlobal ( tmp ) ;
6870 }
6971 }
7072
@@ -126,5 +128,20 @@ internal IntPtr Ptr
126128 return structurePtr ;
127129 }
128130 }
131+
132+ protected void ZeroPtr ( )
133+ {
134+ if ( this . isDirect )
135+ {
136+ this . allocatedData = IntPtr . Zero ;
137+ }
138+ else if ( this . allocatedData != IntPtr . Zero )
139+ {
140+ unsafe
141+ {
142+ * ( IntPtr * ) this . allocatedData = IntPtr . Zero ;
143+ }
144+ }
145+ }
129146 }
130147}
Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ internal MI_Value() : base(true)
7777 this . Dispose ( false ) ;
7878 }
7979
80- internal void Dispose ( )
80+ public override void Dispose ( )
8181 {
8282 this . Dispose ( true ) ;
8383 GC . SuppressFinalize ( this ) ;
@@ -105,6 +105,8 @@ private void Dispose(bool disposing)
105105 this . allocatedData = IntPtr . Zero ;
106106 }
107107 }
108+
109+ base . Dispose ( ) ;
108110 }
109111
110112 internal string String
You can’t perform that action at this time.
0 commit comments