You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider implementing Dispose pattern for handle typedefs #596
When a typedef struct represents a releasable handle, we could implement IDisposable so that C# using syntax would allow deterministic release of these handle.
Reacted by Union Palenshus, Joseph Musser, D-Kay and Michael Hawker
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.
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?
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.
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.
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.
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:
Activity
elachlan commentedon Jul 25, 2022
I think this will be blocked by the solution in #610, which should establish how to reliably identify what nativedef structs are handles.
AArnott commentedon Jul 25, 2022
I don't think so... #610 is about picking which structs should get an
IsNullproperty, but this issue only needs to know whether a given typedef struct has a releaser method, which we already know.elachlan commentedon Jul 25, 2022
Yes, I guess we have to know HOW to release it, to be able to release it. Adding this won't be too bad.
elachlan commentedon Jul 26, 2022
Should this be able to be turned on/off by a setting? Or would there be no benefit in that?
AArnott commentedon Jul 26, 2022
I don't anticipate any downside to it, so I wouldn't add a switch unless a downside presents itself.
elachlan commentedon Aug 17, 2022
Andrew Arnott (@AArnott) if we add
IDisposableto a struct, do we also need to add anbool ownsHandlesimilar tosafehandles? That way we can handle when a method is tagged withDoNotReleaseAttribute?jnm2 commentedon Aug 17, 2022
The handle struct would no longer be blittable and would also be subject to tearing.
elachlan commentedon Aug 17, 2022
So we can't use
IDisposablethen on any struct that is used by a method which has theDoNotReleaseAttribute.AArnott commentedon Aug 18, 2022
No, it's not the
IDisposablethat would present the problems that Joseph Musser (@jnm2) calls out. It's the additional field where we would store theownsHandlevalue.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.
elachlan commentedon Aug 18, 2022
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.AArnott commentedon Aug 19, 2022
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.michael-hawker commentedon Aug 11, 2025
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: