-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
Segfault with WinRT hosting #643
Comments
I'm surprised by this error message. I wonder if I can nerd-snipe @kennykerr into sharing some thoughts here? He's written a great article on this (cached here, his home page seems to be down https://cc.bingj.com/cache.aspx?q=CoIncrementMTAUsage+vs+Roinitialize&d=4841149887960635&mkt=en-US&setlang=en-US&w=AsmFBigDdQ-ZgFRX0qBS_Au0frYV0p1i). Unfortunately, it tantalizingly defers explanation of
|
Hey Tim, well wordpress completely destroyed my blog so I'm left trying to figure out how to set up a static blog from markdown but its proving harder than I'd like. 😊 Anyway, the differences between the various init/uninit functions aren't relevant outside a very small niche of apps and app models that are largely irrelevant today. I'd just do what cppwinrt and windows-rs do and call The only reliable way to handle this is to call https://twitter.com/jamesmcnellis/status/707650369073995776 😉 |
Oh, you did ask about the actual difference. I talked about that here: https://web.archive.org/web/20221128052910/https://kennykerr.ca/2018/04/03/cppwinrt-understanding-coroutines-and-the-calling-context/ |
You remain my hero, @kennykerr! Thank you so much for validating the approach here. And I'm reading your second article now. |
I came across this today. You should be able to reproduce this with the following cmd script:
(You may need to run this script several times 😄)
While running this script, you should see either a segfault or an error like this in some random test:
I think this is caused by calls to the
winrtUninitialize()
helper function which callsRoUninitialize()
. I can't reproduce this if I comment out the call toRoUninitialize()
in the winrtUninitialize().I have looked at the other WinRT projections to see if they call
RoUninitialize()
and I can confirm that they don't. In fact, they don't callRoInitialize()
either. They support automatic initialization of the Windows Runtime by using CoIncrementMTAUsage API.Here is the relevant snippet from
cppwinrt
:https://github.com/microsoft/cppwinrt/blob/fe304096fa30583f3cc2ebfdbf564d2b4081e2ad/strings/base_activation.h#L37-L52
And this is from
windows-rs
:https://github.com/microsoft/windows-rs/blob/a8b3b3841ae5f966dd5f86397a0a81eaa26470b6/crates/libs/windows/src/core/factory_cache.rs#L56-L73
Basically, if the call to
RoGetActivationFactory
fails with the error codeCO_E_NOTINITIALIZED
, they initialize a Multi-Threaded Apartment (MTA) on the current thread by callingCoIncrementMTAUsage()
first and then try again.I think we should do the same, Created #642 for this. Curious to hear your thoughts.
Note that I forgot to mention in #623, before we make NativeFinalizer call IUnknown's
release()
automatically on objects we would need to stop callingwinrtUninitialize()
because, by the time NativeFinalizer callbacks run, the WinRT would be uninitialized which would crash the app (I have experienced this multiple times while working on #623). So, even if we didn't have this problem, we would have to stop usingwinrtUninitialize
at some point.Originally posted by @halildurmus in #630 (comment)
The text was updated successfully, but these errors were encountered: