-
Notifications
You must be signed in to change notification settings - Fork 247
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
Bug: co_await apartment_context crashs with ''CoInitialize has not been called.'' #1415
Comments
This example program does not initialize the MTA in the process. The background tasks therefore run outside the MTA and the "you didn't initialize COM" error is raised. The call to I think the appropriate fix would be to have your code initialize the MTA before it tries to use it. One way that can be done is by calling CoIncrementMTAUsage. Simply call that before the call to |
Thank you. std::thread{ []() {
winrt::init_apartment();
::Sleep(10000);
} }.detach();
std::thread{ []() {
auto type = winrt::impl::get_apartment_type();
printf("%d\n",type.first);
} }.join(); I think once a thread is initialized as MTA. All thread not initialized will be considered as MTA. |
But I find that initialized with single threaded will also make the ContextCallback success. void test()
{
winrt::com_ptr<IContextCallback> ctx;
CoGetObjectContext(IID_IContextCallback, ctx.put_void());
assert(ctx);
TrySubmitThreadpoolCallback([](PTP_CALLBACK_INSTANCE pci, PVOID p) {
auto x = winrt::impl::get_apartment_type();
winrt::com_ptr<IContextCallback> ctx;
*ctx.put_void() = p;
winrt::init_apartment(winrt::apartment_type::single_threaded);
ComCallData data;
HRESULT hr = ctx->ContextCallback([](ComCallData*) {
return S_OK;
}, &data, IID_ICallbackWithNoReentrancyToApplicationSTA, 5, nullptr);
printf("[%d]hr=%d\n", GetCurrentThreadId(), hr);
}, ctx.detach(), 0);
} |
Version
C++/WinRT v2.0.220110.5
Summary
No response
Reproducible example
Expected behavior
no crash.
Actual behavior
crash at
co_await ctx;
with 'CoInitialize has not been called.'Additional comments
Uncomment the
foo()
, await any async windows runtime function, and laterco_await ctx
will no longer crash.Or call
winrt::init_apartment()
afterwinrt::resume_background()
The text was updated successfully, but these errors were encountered: