Description
Actual behavior
GetModuleHandle returns FreeLibrarySafeHandle. Which will call FreeLibrary on cleanup. But, according to the documentation, this is not necessary to avoid errors:
The
GetModuleHandlefunction returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to theFreeLibraryfunction, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned byGetModuleHandleto theFreeLibraryfunction. Doing so can cause a DLL module to be unmapped prematurely.
https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlew#remarks
GetModuleHandleEx partly also affected by this problem, because the cleanup behavior depends on the arguments passed
https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw#parameters
Expected behavior
A clear and concise description of what you expected to happen.
Repro steps
NativeMethods.txtcontent:
GetModuleHandle
NativeMethods.jsoncontent (if present):
- Any of your own code that should be shared?
Context
- CsWin32 version: [e.g.
0.1.635-beta] - Win32Metadata version (if explicitly set by project):
- Target Framework: [e.g.
netstandard2.0] LangVersion(if explicitly set by project): [e.g.9]
Activity
AArnott commentedon May 6, 2022
Great feedback, thank you. Moving to the metadata repo so we can fix it at the right place for all projections.
elachlan commentedon Aug 3, 2022
Mike Battista (@mikebattista) This issue also affects winforms.
mikebattista commentedon Aug 3, 2022
What is the specific change in the metadata? It currently returns an
HINSTANCEwhich hasRAIIFree("FreeLibrary"). Do you expect a different return type that has noRAIIFree? What about for the -Ex API where the behavior is dependent on the arguments?elachlan commentedon Aug 3, 2022
For CsWin32, I guess if the meta data flagged
GetModuleHandlewith an attribute to exclude theRAIIFree, then we can code for it. Otherwise, the only other way to do it is to have a different return type. I am definitely not an expert in this area though.GetModuleHandleExis even harder. Because its only when it uses certain parameters that it needs to callFreeLibrary. We will need to add conditional generation around that to handle it.Andrew Arnott (@AArnott) knows better than myself.
AArnott commentedon Aug 4, 2022
I feel like this has come up before, and if so we may already have a way to express the simple case in metadata. I think
GetModuleHandleshould still return anHINSTANCEso that type equivalence is there, but a[return: NoFree]attribute on the method could indicate that the caller doesn't own the returned value and thus doesn't need to call theRAIIFreemethod. In CsWin32 we would interpret this and create aSafeHandlewhereownsHandle: falseto avoid theFreeLibrarycall.For
GetModuleHandleEx, we may just have to assume it must be released. Methods whose return values vary by argument can be arbitrary complex and I don't know that we want to try to represent them all in metadata.mikebattista commentedon Aug 4, 2022
Do we use
[DoNotRelease]for this? I see that applied toGetCurrentProcessandGetProcessHeap's return handles.I will leave it as is.
elachlan commentedon Aug 5, 2022
https://github.com/microsoft/win32metadata/blob/42ce1543a9d83a1a6cc03c71e3ca84d8854a1c01/generation/WinSDK/emitter.settings.rsp#L491-L492
They both have
[DoNotRelease], Do we handle for that in CsWin32?mikebattista commentedon Aug 5, 2022
Assigning to Andrew Arnott (@AArnott) to resolve whether this is a metadata issue or C#/Win32 issue. It would seem that
[DoNotRelease]is properly applied for this scenario and C#/Win32 just needs to honor it.elachlan commentedon Aug 7, 2022
We need something like this, maybe in
DeclareFriendlyOverloads?AArnott commentedon Aug 10, 2022
Wanna take this, Lachlan Ennis (@elachlan)? We might have a helper method that does the loop-search for you.
elachlan commentedon Aug 10, 2022
I did implement it and it worked. But not exactly how I'd hoped. Since there was still a friendly overloads, when it probably shouldn't be generated at all.
Also if we implement IDisposable on the structs(#596), then we are pushing the problem down the line. I am not sure how to fix that. Maybe wrap the return type?
AArnott commentedon Aug 10, 2022
The friendly overload should still return a
SafeHandle, since all the other friendly APIs we generate will take a SafeHandle as an argument. But the SafeHandle should be created, passing inownsHandle: falseto the constructor.elachlan commentedon Aug 10, 2022
ahhhh, thanks. I should be able to get it sorted in the next few days in my free time.