Skip to content

[API Proposal]: GCHandle.GetTargetUnsafe() with no handle validation #94113

Closed as not planned

Description

@Sergio0694

Background and motivation

GCHandle is often used in scenarios (such as handwritten COM objects, or other interop setups) where callers have complete control over the handles, and nobody else is ever mutating them. The only way, currently, to get a target, is to use GCHandle.Target, which always incurs in a validation check, which is pretty much always just useless branching in these cases.

This proposal is about adding a way to get the handle target with no branches, when callers know no validation is needed.

API Proposal

namespace System.Runtime.InteropServices;

public struct GCHandle
{
+   public readonly object? GetTargetUnsafe();
}

API Usage

// In a constructor (or wherever)
this.handle = GCHandle.Alloc(myObj);

// Later on
object? target = this.handle.GetTargetUnsafe();

Alternative Designs

Alternatively (as @tannergooding also suggested), we could instead just remove the validation check from GCHandle.Target. This would technically be a breaking change, as it would cause invalid handles to throw a NullReferenceException instead of an InvalidOperationException, but it might be considered acceptable in this scenario, since it's a niche API.

The benefit of this approach is that no new API is needed, and everyone "gets faster for free".

Risks

With the new API, no risk.
With the alternate design, people taking a hard dependency on that exception type would break.

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Oct 27, 2023
  2. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Oct 27, 2023
  3. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Oct 27, 2023
  4. tannergooding commented on Oct 27, 2023

    @tannergooding
    Member

    Alternatively (as @tannergooding also suggested), we could instead just remove the validation check from GCHandle.Target. This would technically be a breaking change, as it would cause invalid handles to throw a NullReferenceException instead of an InvalidOperationException, but it might be considered acceptable in this scenario, since it's a niche API.

    Notably this suggestion came from the perspective that the current implementation is logically:

    ThrowIfInvalid(handle);
    handle &= ~(nuint)(1); // strip the pin bit
    return InternalGet(handle);

    This means that these two calls should be logically the same, but they aren't:

    GCHandle.FromIntPtr(0).Target; // InvalidOperation
    GCHandle.FromIntPtr(1).Target; // NullReference

    We're already handling all kinds of invalid handles and most of them, outside 0, result in a NRE. So it might make sense to simply have 0 also cause a NRE. This removes the branch and makes the API overall more consistent.

  5. added this to the Future milestone on Oct 27, 2023
  6. jkotas commented on Oct 27, 2023

    @jkotas
    Member

    There are other similar inefficiencies in GCHandle:

    And there is the cosmetic issue of GCHandle not implementing IDisposable (#54792).

    We should consider all of them at the same time. We may want to consider introducing a new type that has all of them fixed.

  7. Sergio0694 commented on Oct 27, 2023

    @Sergio0694
    ContributorAuthor

    That type looks really nice 👀

    If there is interest, what about opening a new proposal for that, linking all these ones, and try to get that to API review?

  8. Sergio0694 commented on Dec 24, 2024

    @Sergio0694
    ContributorAuthor

    Superseded by #94134, closing this.

  9. locked and limited conversation to collaborators on Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions