Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Apply mitigation to CimInstance disposal race condition #14

Merged
merged 3 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Microsoft.Management.Infrastructure/CimInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ private void Dispose(bool disposing)
{
if (this.InstanceHandle != null && !this.InstanceHandle.IsNull)
{
this.InstanceHandle.Delete();
using (this.InstanceHandle)
{
this.InstanceHandle.Delete();
}
}
}
}
Expand Down
17 changes: 1 addition & 16 deletions src/Microsoft.Management.Infrastructure/Native/MI_Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.Management.Infrastructure.Native
{
internal class MI_Instance : MI_NativeObjectWithFT<MI_Instance.MI_InstanceFT>
internal class MI_Instance : MI_NativeObjectWithFT<MI_Instance.MI_InstanceFT>, IDisposable
{
internal MI_Result GetElement(
string name,
Expand Down Expand Up @@ -124,21 +124,6 @@ internal MI_Result Delete()
return localResult;
}

private void ZeroPtr()
{
if (this.isDirect)
{
this.allocatedData = IntPtr.Zero;
}
else if (this.allocatedData != IntPtr.Zero)
{
unsafe
{
*(IntPtr*)this.allocatedData = IntPtr.Zero;
}
}
}

internal MI_Result IsA(
MI_ClassDecl classDecl,
out bool flag
Expand Down
23 changes: 20 additions & 3 deletions src/Microsoft.Management.Infrastructure/Native/MI_NativeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.Management.Infrastructure.Native
{
internal abstract class MI_NativeObject
internal abstract class MI_NativeObject : IDisposable
{
[StructLayout(LayoutKind.Sequential, CharSet = MI_PlatformSpecific.AppropriateCharSet)]
protected struct MI_NativeObjectNormalMembersLayout
Expand Down Expand Up @@ -60,11 +60,13 @@ internal struct ArrayPtr
protected IntPtr allocatedData;
protected bool isDirect;

~MI_NativeObject()
public virtual void Dispose()
{
if (this.allocatedData != IntPtr.Zero)
{
Marshal.FreeHGlobal(this.allocatedData);
IntPtr tmp = this.allocatedData;
this.ZeroPtr();
Marshal.FreeHGlobal(tmp);
}
}

Expand Down Expand Up @@ -126,5 +128,20 @@ internal IntPtr Ptr
return structurePtr;
}
}

protected void ZeroPtr()
{
if (this.isDirect)
{
this.allocatedData = IntPtr.Zero;
}
else if (this.allocatedData != IntPtr.Zero)
{
unsafe
{
*(IntPtr*)this.allocatedData = IntPtr.Zero;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal MI_Value() : base(true)
this.Dispose(false);
}

internal void Dispose()
public override void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
Expand Down Expand Up @@ -105,6 +105,8 @@ private void Dispose(bool disposing)
this.allocatedData = IntPtr.Zero;
}
}

base.Dispose();
}

internal string String
Expand Down