Skip to content

HDC's [RAIIFree] attribute names ReleaseDC but that function has two parameters #423

Closed

Description

@kennykerr

I would expect free functions to have a single parameter.

Activity

  1. sotteson1 commented on Apr 15, 2021

    @sotteson1
    Contributor

    Yeah, I wasn't sure how to handle that one. Any ideas?

  2. kennykerr commented on Apr 15, 2021

    @kennykerr
    ContributorAuthor

    🤔 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.

  3. added
    broken api
    on Apr 15, 2021
  4. AArnott commented on Apr 17, 2021

    @AArnott
    Member

    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)

  5. kennykerr commented on Apr 20, 2021

    @kennykerr
    ContributorAuthor

    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

  6. AArnott commented on Apr 20, 2021

    @AArnott
    Member

    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.

  7. kennykerr commented on Apr 20, 2021

    @kennykerr
    ContributorAuthor

    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?

  8. AArnott commented on Apr 20, 2021

    @AArnott
    Member

    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.

  9. kennykerr commented on Apr 20, 2021

    @kennykerr
    ContributorAuthor

    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.

  10. AArnott commented on Apr 20, 2021

    @AArnott
    Member

    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.

  11. weltkante commented on Jun 13, 2021

    @weltkante

    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)

  12. mikebattista commented on Jun 14, 2021

    @mikebattista
    Contributor

    So is the conclusion here to:

    1. Remove [RAIIFree] from HDC?
    2. Create a clone of CreatedHDC called AcquiredHDC with [RAIIFree("ReleaseDC")] and apply that to GetDC?
    3. Update CreateIC* to return CreatedHDC (CreateIC* should return CreatedHDC #528)?
  13. kennykerr commented on Jun 14, 2021

    @kennykerr
    ContributorAuthor

    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.

  14. kennykerr commented on Feb 10, 2022

    @kennykerr
    ContributorAuthor

    Just closing this old issue as I have no plans to use this attribute.

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

    broken api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions