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
HDC's [RAIIFree] attribute names ReleaseDC but that function has two parameters #423
🤔 I suspect when there are cases like this where we can't just automate the correct behavior that we need to get out the way and let the caller code this by hand. So just dropping the RAIIFree attribute in this case.
IMO we should keep the attribute, and let projections figure out the release function is one they don't know how to invoke. CsWin32 for example always performs this check because we encountered this before.
What value does the attribute add in such cases? It would seem that every language projection would then have to be updated to ignore such attributes, which is costly:
find attribute -> find function -> parse signature -> function cannot be called -> give up
It would seem that every language projection would then have to be updated to ignore such attributes
Alternatively they could learn the nuances of how to call these specially and do so. And the value of the attribute in such a case is to highlight to the projection author where such special cases exist to be won.
I guess I don't understand how a language projection would "learn" to call ReleaseDC? In both C++ and Rust it would need to be called from a "destructor" with not possibility for the caller to provide additional values. How would the destructor be generated with the correct values for the additional parameters?
GetDC's return value should be closed with ReleaseDC. ReleaseDC takes both the input arg to GetDC and its return value. A projection could prepare a disposable/destructible result from GetDC that captures both of these value.
In particular, CsWin32 could create the ReleaseDCSafeHandle class to take the HWND value as well as the HDC handle in its constructor.
So ya, a projection author would have to special case it, but without the attributes on these methods that require special handling, I wouldn't even know it was something I could do to improve my projection.
Hmm, that assumes knowledge of both the creation function and the destruction function and how to pair them together. Now we're getting into a world where it might be better to model this as a class with constructors, destructors, and so forth.
Anyway, in both C++ and Rust the struct cannot carry more than the memory layout of the struct/handle itself. There is no room for additional fields. They are both designed with the assumption that the memory layout of the projected type matches that of the ABI type.
In C# my extern method would just be HDC (or whatever the typedef is called). But my "friendly overload" that I generate to return the SafeHandle would be what captures the input and returns the richer output.
that assumes knowledge of both the creation function and the destruction function and how to pair them together
Yup. It would require the projection to specially recognize the creation function. So the attribute isn't required by the time the projection has been trained for that method -- the attribute only serves as a clue to the projection author that something could be done here.
The destruction function doesn't really need special recognition. The projection can see that the function signature isn't common and skip handling.
Note that there exist plenty HDC that are returned from various functions, or passed as arguments into callbacks, where you are just sharing the HDC and are never supposed to release them. I think GetDC is like CreateDC in that it needs a special return type, maybe an AcquiredHDC matching the CreatedHDC and leave the normal HDC without RAII annotations?
For example BeginPaint and GetBufferedPaintDC return a HDC that is not supposed to be treated as RAII via ReleaseDC. I'm sure there are a few more variations like retrieving an HDC through a COM API by passing a HDC field byref (since return values are reserved for HRESULTs). Also HDCs stored in struct fields probably should not be treated as RAII resources, otherwise this could become weird.
So I'm personally for removing the RAII annotation from HDC because it doesn't seem correct given the existing API spectrum. If you think that ReleaseDC is valuable to identify for advanced RAII then it should be a separate struct similar to CreatedHDC. (I do think projections can make use of those annotations, even if they don't create a RAII destructor they could identify when you forget to use ReleaseDC, but I agree there's probably no language which would immediately make use of it due to its complexity)
As I originally created this issue, I'll just add that I haven't started using RAIIFree in Rust because of #389 and #423 making it too hard to experiment so I can't really comment on what would be best.
Activity
sotteson1 commentedon Apr 15, 2021
Yeah, I wasn't sure how to handle that one. Any ideas?
kennykerr commentedon Apr 15, 2021
🤔 I suspect when there are cases like this where we can't just automate the correct behavior that we need to get out the way and let the caller code this by hand. So just dropping the
RAIIFreeattribute in this case.AArnott commentedon Apr 17, 2021
IMO we should keep the attribute, and let projections figure out the release function is one they don't know how to invoke. CsWin32 for example always performs this check because we encountered this before.
See also #25 (comment)
kennykerr commentedon Apr 20, 2021
What value does the attribute add in such cases? It would seem that every language projection would then have to be updated to ignore such attributes, which is costly:
find attribute -> find function -> parse signature -> function cannot be called -> give upAArnott commentedon Apr 20, 2021
Alternatively they could learn the nuances of how to call these specially and do so. And the value of the attribute in such a case is to highlight to the projection author where such special cases exist to be won.
kennykerr commentedon Apr 20, 2021
I guess I don't understand how a language projection would "learn" to call
ReleaseDC? In both C++ and Rust it would need to be called from a "destructor" with not possibility for the caller to provide additional values. How would the destructor be generated with the correct values for the additional parameters?AArnott commentedon Apr 20, 2021
GetDC's return value should be closed withReleaseDC.ReleaseDCtakes both the input arg toGetDCand its return value. A projection could prepare a disposable/destructible result fromGetDCthat captures both of these value.In particular, CsWin32 could create the
ReleaseDCSafeHandleclass to take theHWNDvalue as well as theHDChandle in its constructor.So ya, a projection author would have to special case it, but without the attributes on these methods that require special handling, I wouldn't even know it was something I could do to improve my projection.
kennykerr commentedon Apr 20, 2021
Hmm, that assumes knowledge of both the creation function and the destruction function and how to pair them together. Now we're getting into a world where it might be better to model this as a class with constructors, destructors, and so forth.
Anyway, in both C++ and Rust the struct cannot carry more than the memory layout of the struct/handle itself. There is no room for additional fields. They are both designed with the assumption that the memory layout of the projected type matches that of the ABI type.
AArnott commentedon Apr 20, 2021
In C# my extern method would just be
HDC(or whatever the typedef is called). But my "friendly overload" that I generate to return theSafeHandlewould be what captures the input and returns the richer output.Yup. It would require the projection to specially recognize the creation function. So the attribute isn't required by the time the projection has been trained for that method -- the attribute only serves as a clue to the projection author that something could be done here.
The destruction function doesn't really need special recognition. The projection can see that the function signature isn't common and skip handling.
weltkante commentedon Jun 13, 2021
Note that there exist plenty
HDCthat are returned from various functions, or passed as arguments into callbacks, where you are just sharing the HDC and are never supposed to release them. I thinkGetDCis likeCreateDCin that it needs a special return type, maybe anAcquiredHDCmatching theCreatedHDCand leave the normalHDCwithout RAII annotations?For example BeginPaint and GetBufferedPaintDC return a HDC that is not supposed to be treated as RAII via ReleaseDC. I'm sure there are a few more variations like retrieving an HDC through a COM API by passing a HDC field byref (since return values are reserved for HRESULTs). Also HDCs stored in struct fields probably should not be treated as RAII resources, otherwise this could become weird.
So I'm personally for removing the RAII annotation from HDC because it doesn't seem correct given the existing API spectrum. If you think that ReleaseDC is valuable to identify for advanced RAII then it should be a separate struct similar to CreatedHDC. (I do think projections can make use of those annotations, even if they don't create a RAII destructor they could identify when you forget to use ReleaseDC, but I agree there's probably no language which would immediately make use of it due to its complexity)
mikebattista commentedon Jun 14, 2021
So is the conclusion here to:
kennykerr commentedon Jun 14, 2021
As I originally created this issue, I'll just add that I haven't started using
RAIIFreein Rust because of #389 and #423 making it too hard to experiment so I can't really comment on what would be best.kennykerr commentedon Feb 10, 2022
Just closing this old issue as I have no plans to use this attribute.