Skip to content

Consider implementing Dispose pattern for handle typedefs #596

Closed as not planned

Description

When a typedef struct represents a releasable handle, we could implement IDisposable so that C# using syntax would allow deterministic release of these handle.

Activity

  1. elachlan commented on Jul 25, 2022

    @elachlan
    Contributor

    I think this will be blocked by the solution in #610, which should establish how to reliably identify what nativedef structs are handles.

  2. AArnott commented on Jul 25, 2022

    @AArnott
    MemberAuthor

    I don't think so... #610 is about picking which structs should get an IsNull property, but this issue only needs to know whether a given typedef struct has a releaser method, which we already know.

  3. elachlan commented on Jul 25, 2022

    @elachlan
    Contributor

    Yes, I guess we have to know HOW to release it, to be able to release it. Adding this won't be too bad.

  4. elachlan commented on Jul 26, 2022

    @elachlan
    Contributor

    Should this be able to be turned on/off by a setting? Or would there be no benefit in that?

  5. AArnott commented on Jul 26, 2022

    @AArnott
    MemberAuthor

    I don't anticipate any downside to it, so I wouldn't add a switch unless a downside presents itself.

  6. elachlan commented on Aug 17, 2022

    @elachlan
    Contributor

    Andrew Arnott (@AArnott) if we add IDisposable to a struct, do we also need to add an bool ownsHandle similar to safehandles? That way we can handle when a method is tagged with DoNotReleaseAttribute?

  7. jnm2 commented on Aug 17, 2022

    @jnm2
    Contributor

    The handle struct would no longer be blittable and would also be subject to tearing.

  8. elachlan commented on Aug 17, 2022

    @elachlan
    Contributor

    So we can't use IDisposable then on any struct that is used by a method which has the DoNotReleaseAttribute.

  9. AArnott commented on Aug 18, 2022

    @AArnott
    MemberAuthor

    No, it's not the IDisposable that would present the problems that Joseph Musser (@jnm2) calls out. It's the additional field where we would store the ownsHandle value.
    If folks want .NET/C# to help with disposal of native resources, SafeHandle is the standard. The typedef structs are the native primitive that folks use when they specifically want to control lifetime themselves and/or cannot take the GC pressure of a SafeHandle. To make the struct disposable may be interesting, but it blurs the line. And if it led to analyzers producing warnings when they are not disposed of, that would violate a major point of falling back to them in the first place.

    So maybe we could do the IDisposable struct thing if we new analyzers don't and would never call out disposing them. But I'm leaning against doing it at all given that's SafeHandle territory.

  10. elachlan commented on Aug 18, 2022

    @elachlan
    Contributor

    That is probably a good idea. So should we close this issue or limit it to structs without associated methods tagged with DoNotReleaseAttribute? I don't really have an opinion either way.

  11. AArnott commented on Aug 19, 2022

    @AArnott
    MemberAuthor

    Given the risk that an analyzer exists or comes long that would try to coerce people to dispose of their typedef structs, I'd lean toward not implementing IDisposable, and continue to collect feedback.

  12. michael-hawker commented on Aug 11, 2025

    @michael-hawker

    Was just translating some code from another PInvoke library to CsWin32, it was nice they handled the Disposable from CreateRectRgn on the HGRN object. That way they could just use C#'s using statement handle everything in scope:

    using var rgn = PInvoke.CreateRectRgn(-2, -2, -1, -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions