diff --git a/src/corelib/kernel/qeventdispatcher_winrt.cpp b/src/corelib/kernel/qeventdispatcher_winrt.cpp index 600c6c38fd1..f7a1f969a8f 100644 --- a/src/corelib/kernel/qeventdispatcher_winrt.cpp +++ b/src/corelib/kernel/qeventdispatcher_winrt.cpp @@ -44,12 +44,13 @@ #include #include #include -#include #include #include #include #include +#include + #include #include #include @@ -300,19 +301,19 @@ HRESULT QEventDispatcherWinRT::runOnMainThread(const std::function &d if (QThread::currentThread() == QCoreApplication::instance()->thread()) return delegate(); - auto semaphore = QSharedPointer(new QSemaphore); - auto ptrSemaphore = new QSharedPointer(semaphore); - auto result = QSharedPointer(new HRESULT); - auto ptrResult = new QSharedPointer(result); + struct State { + QSemaphore semaphore; + HRESULT result; + }; - QMetaObject::invokeMethod(QCoreApplication::instance(), [delegate, ptrSemaphore, ptrResult]() { - **ptrResult = delegate(); - delete ptrResult; - (*ptrSemaphore)->release(); - delete ptrSemaphore; + const auto state = std::make_shared(); + + QMetaObject::invokeMethod(QCoreApplication::instance(), [delegate, state]() { + const QSemaphoreReleaser releaser{state->semaphore}; + state->result = delegate(); }, nullptr); - return semaphore->tryAcquire(1, timeout) ? *result : E_FAIL; + return state->semaphore.tryAcquire(1, timeout) ? state->result : E_FAIL; } bool QEventDispatcherWinRT::processEvents(QEventLoop::ProcessEventsFlags flags)