-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marshal.ReleaseComObject does not account for ComWrappers #54317
Comments
@kant2002 This is by design. The appropriate API to call instead is |
I'm hit that when experimenting how far can I go with ComWrappers in WPF (to see NativeAOT ability potentially) WPF retreive object using This is quick summary on the libraries which use this functions. I count only files, because it's easier to do in the browser. https://github.com/dotnet/winforms/search?q=ReleaseComObject 6 non test files https://github.com/dotnet/runtime/search?q=ReleaseComObject Other files. WinForms almost not impacted if not using ActiveX. It works perfectly fine for me in NativeAOT in relatively complex app. |
@kant2002 Rather than updating this API I would recommend converting entirely to |
I agree that the ultimate goal should be to get to 100% ComWrappers, but I think it would make sense to update APIs like Supporting ComWrappers in APIs like |
I don't think it costs very little. It is a complicated scenario with a fair bit of nuance.
The above would mean (1) when and how this works is complicated and (2) the performance relative to the existing behavior (i.e., built-in) is much more and that defeats the purpose of I would argue we should separate them at this point and rely on either an |
@kant2002 I took another look at this and really am concerned about the complexity here. We would need to update both An argument could be made this is an advanced scenario so that is the cost. I'm sympathetic to this perspective actually. However, the arguments I am hearing are around successfully transitioning from the built-in system. This means we want to guarantee semantics to some degree and I don't see how we can do that. I will say that as we move forward with I would recommend doing the following prior to these calls: if (Marshal.IsComObject(obj))
Marshal.ReleaseComObject(obj); |
@AaronRobinsonMSFT |
Doh! All those years of seeing that in the VS debugger and I never realized it was marked @jkoritzinsky I've updated the example so it will work now but I do agree we should perhaps talk more about the needs here. |
I'm fine with either IDisposable, finalizer or other custom interface, but I would like that somehow this function work almost like it is expected in that function. I personally think about this interface
|
The global ComWrappers instance is designed to only able to cover the simplest COM interop cases transparently (simple interface marshaling). It won't cover existing libraries and application that use COM interop in more advanced ways. It is very likely that applications or libraries that are using COM interop heavily will require changes to work well with ComWrappers (either global or local instance).
This will prevent the app from crashing with ComWrappers, but it won't make it work well. The code was presumably explicitly releasing the object for a reason, and not just because of it can. I think it would need to be:
and the respective ComWrappers would have to implement |
If nothing would be added/changed, I can provide separate PR's for each library in this repo. Any objections? |
The challenge with changes like this is testing - how do we know that it actually works? I think it would preferable to work towards the 100% ComWrappers solution: Pick one of these libraries, figure out how to make wean it off of the built-in COM support by default, and make it happen. Rinse & repeat. |
Let's see. I would pick System.Drawing.Common as easiest target with only 2 interfaces. IStream and IPicture. |
Yes, I started working on moving System.Drawing.Common over to COM wrappers in order to make it trim compatible. Here are the current ILLink warnings in Drawing: runtime/src/libraries/System.Drawing.Common/src/ILLink/ILLink.Suppressions.xml Lines 16 to 81 in cff5854
They are all:
Moving them to use COM wrappers will resolve these ILLink warnings. |
@kant2002 - if you want, you could pick up one of the other libraries? https://github.com/dotnet/runtime/search?q=IL2050
|
@eerhardt For now I would like to shadow you, to see how ComWrappers should be implemented. Can you tag me if you have PR or draft? Is there general consensus how it should be done? I can start poking into System.Data.Odbc or System.Management based on what should be done. Want to limit scope to easiest target first. |
For sure! I'll tag you when I have a PR ready. Probably sometime next week. |
FYI - the first round of converting System.Drawing.Common to ComWrappers: #54636. |
Does System.Speech not appear in search for IL2050 because it uses |
Remove unused functions which trigger IL2050 As per discussed in dotnet#54317
No, we choose not to analyze System.Speech for trimmability yet, because it isn't a very used assembly (it only got supported on .NET Core in the 5.0 time frame). Here is the list of assemblies we ignore for trimmability (for now): runtime/src/libraries/illink-oob.targets Lines 15 to 25 in bde0322
Some of that reasoning is listed here: #52272 (comment) |
* Remove IL2050 in System.Management Remove unused functions which trigger IL2050 As per discussed in #54317 * Fix location of variables
I think the follow up here should be documentation regarding contracts and how implementers should consume |
This behavior has been documented in detail in the official walkthrough -https://docs.microsoft.com/en-us/dotnet/standard/native-interop/tutorial-comwrappers#step-4--handle-native-object-wrapper-lifetime-details. |
I try to make WPF work using ComWrappers only, and hit followin road block. Currently RCW in WPF released using
Marshal.ReleaseComObject
. This method does not take into account that some code is using ComWrappersI think in spirit of #50500 that behaviour should change.
/cc @AaronRobinsonMSFT
The text was updated successfully, but these errors were encountered: