-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
Description
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 nullalso avoids the issue.
Expected Result:
- I would like to understand the cause of this issue
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
No status