Skip to content

Calling GCHandle.Free causes other handles to be released #112727

@Liangjia0411

Description

@Liangjia0411

Environment Information:

  • dotnet version: 9.0.1
  • Virtual machine: coreclr

Sample Code:

public ref struct GCHandleScope : IDisposable
{
	private GCHandle _handle;

	public GCHandleScope(object target, GCHandleType handleType = GCHandleType.Normal)
	{
		if (target != null)
			_handle = GCHandle.Alloc(target, handleType);
	}

	public readonly IntPtr GetIntPtr() => GCHandle.ToIntPtr(_handle);

	public void Dispose()
	{
		if (_handle.IsAllocated)
		{
			_handle.Free();
		}
	}
}

// User Code
public static void UserFunc()
{
	using (var scope = new GCHandleScope(new object()))
	{
		var ptr = scope.GetIntPtr();
		
		// do something with ptr

		GCHandle.FromIntPtr(ptr).Free();
	}
}

Reproduction Steps:

  • Create a custom GCHandleScope to manage the lifecycle of a GCHandle.
  • Externally release this handle using GCHandle.FromIntPtr(ptr).Free().
  • During Dispose, it may release other handles, causing the handles held by native code to become invalid.

Additional Information:

  • Changing the handleType of GCHandleScope to Weak does not have this issue.
  • Adding a check in Dispose to only Free when _handle.Target is not null also avoids the issue.

Expected Result:

  • I would like to understand the cause of this issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions