From c75705b4b3e47ad5d10bf9950d0187aa54eb5952 Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Wed, 24 Sep 2025 17:59:06 +0300 Subject: [PATCH 1/8] fix windows hang-on --- .../custom_platform_view/graphics_context.cc | 14 +++++++++++++ .../custom_platform_view/graphics_context.h | 1 + .../in_app_webview/in_app_webview_manager.cpp | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc index 2bbc1e748..4d0405892 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc @@ -22,6 +22,20 @@ namespace flutter_inappwebview_plugin valid_ = true; } + GraphicsContext::~GraphicsContext() + { + // Explicitly release DirectX resources to prevent hanging process + if (device_context_) { + device_context_->ClearState(); + device_context_->Flush(); + device_context_ = nullptr; + } + + device_winrt_ = nullptr; + device_ = nullptr; + valid_ = false; + } + winrt::com_ptr GraphicsContext::CreateCompositor() { diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h index 883bffe71..6af731840 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h @@ -12,6 +12,7 @@ namespace flutter_inappwebview_plugin class GraphicsContext { public: GraphicsContext(rx::RoHelper* rohelper); + ~GraphicsContext(); inline bool IsValid() const { return valid_; } diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp index 8a03dbd33..11451f278 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp @@ -256,6 +256,26 @@ namespace flutter_inappwebview_plugin keepAliveWebViews.clear(); windowWebViews.clear(); UnregisterClass(windowClass_.lpszClassName, nullptr); + + // Properly cleanup static DirectX resources to prevent hanging process + if (compositor_) { + // Release the extra reference we added in constructor + compositor_->Release(); + compositor_ = nullptr; + } + + // Reset graphics context to release DirectX resources + graphics_context_.reset(); + + // Cleanup dispatcher queue controller + if (dispatcher_queue_controller_) { + dispatcher_queue_controller_ = nullptr; + } + + // Reset RoHelper + rohelper_.reset(); + + valid_ = false; plugin = nullptr; } } From 465d6b1818bd95c8f16db3bf9c33d0ecfdfa8d0d Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Thu, 25 Sep 2025 14:17:05 +0300 Subject: [PATCH 2/8] fix windows focus loose and gain --- .../src/in_app_webview/custom_platform_view.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/flutter_inappwebview_windows/lib/src/in_app_webview/custom_platform_view.dart b/flutter_inappwebview_windows/lib/src/in_app_webview/custom_platform_view.dart index dbffdd7f3..5d19b850c 100644 --- a/flutter_inappwebview_windows/lib/src/in_app_webview/custom_platform_view.dart +++ b/flutter_inappwebview_windows/lib/src/in_app_webview/custom_platform_view.dart @@ -362,14 +362,14 @@ class _CustomPlatformViewState extends State _reportSurfaceSize(); _reportWidgetPosition(); - if (!_focusNode.hasFocus) { - _focusNode.requestFocus(); - Future.delayed(const Duration(milliseconds: 50), () { - if (!_focusNode.hasFocus) { - _focusNode.requestFocus(); - } - }); - } + // if (!_focusNode.hasFocus) { + // _focusNode.requestFocus(); + // Future.delayed(const Duration(milliseconds: 50), () { + // if (!_focusNode.hasFocus) { + // _focusNode.requestFocus(); + // } + // }); + // } _pointerKind = ev.kind; if (ev.kind == PointerDeviceKind.touch) { From 1410ffaacd48ec4d125a86ae0711ce6754791c26 Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Wed, 1 Oct 2025 12:41:40 +0300 Subject: [PATCH 3/8] Fix WebView2 v140.0.3485.94+ compatibility: Add COM initialization before CreateCoreWebView2EnvironmentWithOptions - Add CoInitializeEx call before CreateCoreWebView2EnvironmentWithOptions as required by WebView2 v140.0.3485.94+ - Handle RPC_E_CHANGED_MODE case when COM is already initialized with COINIT_MULTITHREADED - Proper COM cleanup in both success and failure scenarios - Fixes 'CoInitialize has not been called' error with latest WebView2 runtime - Reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl --- .../webview_environment.cpp | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp index b1285f01c..fb606b6ec 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp @@ -85,12 +85,33 @@ namespace flutter_inappwebview_plugin } } + // Ensure COM is initialized before calling CreateCoreWebView2EnvironmentWithOptions + // This is required by WebView2 v140.0.3485.94 and later versions + // Reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl + HRESULT comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); + bool comWasInitializedByUs = false; + + if (SUCCEEDED(comInitResult)) { + // S_OK means we initialized COM, S_FALSE means it was already initialized + comWasInitializedByUs = (comInitResult == S_OK); + } else if (comInitResult == RPC_E_CHANGED_MODE) { + // COM was already initialized with COINIT_MULTITHREADED + // This is acceptable for WebView2, continue without error + debugLog("COM already initialized with COINIT_MULTITHREADED"); + } else { + // COM initialization failed with some other error + if (completionHandler) { + completionHandler(comInitResult); + } + return; + } + auto hr = CreateCoreWebView2EnvironmentWithOptions( settings && settings->browserExecutableFolder.has_value() ? utf8_to_wide(settings->browserExecutableFolder.value()).c_str() : nullptr, settings && settings->userDataFolder.has_value() ? utf8_to_wide(settings->userDataFolder.value()).c_str() : nullptr, options.Get(), Callback( - [this, hwnd, completionHandler](HRESULT result, wil::com_ptr environment) -> HRESULT + [this, hwnd, completionHandler, comWasInitializedByUs](HRESULT result, wil::com_ptr environment) -> HRESULT { if (succeededOrLog(result)) { environment_ = std::move(environment); @@ -161,11 +182,23 @@ namespace flutter_inappwebview_plugin else if (completionHandler) { completionHandler(result); } + + // Clean up COM if we initialized it + if (comWasInitializedByUs) { + CoUninitialize(); + } + return S_OK; }).Get()); - if (failedAndLog(hr) && completionHandler) { - completionHandler(hr); + if (failedAndLog(hr)) { + if (completionHandler) { + completionHandler(hr); + } + // Clean up COM if we initialized it and the call failed immediately + if (comWasInitializedByUs) { + CoUninitialize(); + } } } From fbfb1cffe6a3ae807d38bcac03e821dc28039d64 Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Wed, 1 Oct 2025 14:12:07 +0300 Subject: [PATCH 4/8] Improve WebView2 COM initialization: Add thread safety and better error handling - Add static mutex to prevent race conditions during COM initialization - Improve COM initialization logic to handle COINIT_MULTITHREADED scenario - Add better logging for debugging COM initialization issues - Handle RPC_E_CHANGED_MODE by attempting to reinitialize with matching threading model - This should fix the mtx_do_lock crash in MSVCP140 runtime --- .../webview_environment.cpp | 53 ++++++++++++++----- .../webview_environment/webview_environment.h | 2 + 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp index fb606b6ec..f0d06c064 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp @@ -10,6 +10,9 @@ namespace flutter_inappwebview_plugin { using namespace Microsoft::WRL; + // Static mutex definition + std::mutex WebViewEnvironment::com_init_mutex_; + WebViewEnvironment::WebViewEnvironment(const FlutterInappwebviewWindowsPlugin* plugin, const std::string& id) : plugin(plugin), id(id), channelDelegate(std::make_unique(this, plugin->registrar->messenger())) @@ -88,23 +91,45 @@ namespace flutter_inappwebview_plugin // Ensure COM is initialized before calling CreateCoreWebView2EnvironmentWithOptions // This is required by WebView2 v140.0.3485.94 and later versions // Reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl - HRESULT comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); + + HRESULT comInitResult; bool comWasInitializedByUs = false; - if (SUCCEEDED(comInitResult)) { - // S_OK means we initialized COM, S_FALSE means it was already initialized - comWasInitializedByUs = (comInitResult == S_OK); - } else if (comInitResult == RPC_E_CHANGED_MODE) { - // COM was already initialized with COINIT_MULTITHREADED - // This is acceptable for WebView2, continue without error - debugLog("COM already initialized with COINIT_MULTITHREADED"); - } else { - // COM initialization failed with some other error - if (completionHandler) { - completionHandler(comInitResult); + // Use mutex to prevent race conditions during COM initialization + { + std::lock_guard lock(com_init_mutex_); + + // First try COINIT_APARTMENTTHREADED as recommended by Microsoft + comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); + + if (SUCCEEDED(comInitResult)) { + // S_OK means we initialized COM, S_FALSE means it was already initialized + comWasInitializedByUs = (comInitResult == S_OK); + debugLog("COM initialized successfully with COINIT_APARTMENTTHREADED"); + } else if (comInitResult == RPC_E_CHANGED_MODE) { + // COM was already initialized with COINIT_MULTITHREADED + // Try to reinitialize with MULTITHREADED to match existing state + CoUninitialize(); // Clean up the failed attempt + comInitResult = CoInitializeEx(nullptr, COINIT_MULTITHREADED); + if (SUCCEEDED(comInitResult)) { + comWasInitializedByUs = (comInitResult == S_OK); + debugLog("COM reinitialized with COINIT_MULTITHREADED to match existing state"); + } else { + debugLog("Failed to reinitialize COM with COINIT_MULTITHREADED"); + if (completionHandler) { + completionHandler(comInitResult); + } + return; + } + } else { + // COM initialization failed with some other error + debugLog("COM initialization failed with HRESULT: " + std::to_string(comInitResult)); + if (completionHandler) { + completionHandler(comInitResult); + } + return; } - return; - } + } // mutex scope ends here auto hr = CreateCoreWebView2EnvironmentWithOptions( settings && settings->browserExecutableFolder.has_value() ? utf8_to_wide(settings->browserExecutableFolder.value()).c_str() : nullptr, diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h index ed3dea4c4..31a1bd9a0 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -41,6 +42,7 @@ namespace flutter_inappwebview_plugin private: wil::com_ptr environment_; WNDCLASS windowClass_ = {}; + static std::mutex com_init_mutex_; }; } #endif //FLUTTER_INAPPWEBVIEW_PLUGIN_WEBVIEW_ENVIRONMENT_H_ \ No newline at end of file From db4fe4906a884861662341ef6274bcaefa2c5366 Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Wed, 1 Oct 2025 14:22:52 +0300 Subject: [PATCH 5/8] Revert "Improve WebView2 COM initialization: Add thread safety and better error handling" This reverts commit fbfb1cffe6a3ae807d38bcac03e821dc28039d64. --- .../webview_environment.cpp | 53 +++++-------------- .../webview_environment/webview_environment.h | 2 - 2 files changed, 14 insertions(+), 41 deletions(-) diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp index f0d06c064..fb606b6ec 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp @@ -10,9 +10,6 @@ namespace flutter_inappwebview_plugin { using namespace Microsoft::WRL; - // Static mutex definition - std::mutex WebViewEnvironment::com_init_mutex_; - WebViewEnvironment::WebViewEnvironment(const FlutterInappwebviewWindowsPlugin* plugin, const std::string& id) : plugin(plugin), id(id), channelDelegate(std::make_unique(this, plugin->registrar->messenger())) @@ -91,45 +88,23 @@ namespace flutter_inappwebview_plugin // Ensure COM is initialized before calling CreateCoreWebView2EnvironmentWithOptions // This is required by WebView2 v140.0.3485.94 and later versions // Reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl - - HRESULT comInitResult; + HRESULT comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); bool comWasInitializedByUs = false; - // Use mutex to prevent race conditions during COM initialization - { - std::lock_guard lock(com_init_mutex_); - - // First try COINIT_APARTMENTTHREADED as recommended by Microsoft - comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - - if (SUCCEEDED(comInitResult)) { - // S_OK means we initialized COM, S_FALSE means it was already initialized - comWasInitializedByUs = (comInitResult == S_OK); - debugLog("COM initialized successfully with COINIT_APARTMENTTHREADED"); - } else if (comInitResult == RPC_E_CHANGED_MODE) { - // COM was already initialized with COINIT_MULTITHREADED - // Try to reinitialize with MULTITHREADED to match existing state - CoUninitialize(); // Clean up the failed attempt - comInitResult = CoInitializeEx(nullptr, COINIT_MULTITHREADED); - if (SUCCEEDED(comInitResult)) { - comWasInitializedByUs = (comInitResult == S_OK); - debugLog("COM reinitialized with COINIT_MULTITHREADED to match existing state"); - } else { - debugLog("Failed to reinitialize COM with COINIT_MULTITHREADED"); - if (completionHandler) { - completionHandler(comInitResult); - } - return; - } - } else { - // COM initialization failed with some other error - debugLog("COM initialization failed with HRESULT: " + std::to_string(comInitResult)); - if (completionHandler) { - completionHandler(comInitResult); - } - return; + if (SUCCEEDED(comInitResult)) { + // S_OK means we initialized COM, S_FALSE means it was already initialized + comWasInitializedByUs = (comInitResult == S_OK); + } else if (comInitResult == RPC_E_CHANGED_MODE) { + // COM was already initialized with COINIT_MULTITHREADED + // This is acceptable for WebView2, continue without error + debugLog("COM already initialized with COINIT_MULTITHREADED"); + } else { + // COM initialization failed with some other error + if (completionHandler) { + completionHandler(comInitResult); } - } // mutex scope ends here + return; + } auto hr = CreateCoreWebView2EnvironmentWithOptions( settings && settings->browserExecutableFolder.has_value() ? utf8_to_wide(settings->browserExecutableFolder.value()).c_str() : nullptr, diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h index 31a1bd9a0..ed3dea4c4 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -42,7 +41,6 @@ namespace flutter_inappwebview_plugin private: wil::com_ptr environment_; WNDCLASS windowClass_ = {}; - static std::mutex com_init_mutex_; }; } #endif //FLUTTER_INAPPWEBVIEW_PLUGIN_WEBVIEW_ENVIRONMENT_H_ \ No newline at end of file From 6b14c3b5819fc17d66e818b5474e9d43efc4d72d Mon Sep 17 00:00:00 2001 From: Alex Melnyk Date: Wed, 1 Oct 2025 14:25:17 +0300 Subject: [PATCH 6/8] Revert "Fix WebView2 v140.0.3485.94+ compatibility: Add COM initialization before CreateCoreWebView2EnvironmentWithOptions" This reverts commit 1410ffaacd48ec4d125a86ae0711ce6754791c26. --- .../webview_environment.cpp | 39 ++----------------- 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp index fb606b6ec..b1285f01c 100644 --- a/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp +++ b/flutter_inappwebview_windows/windows/webview_environment/webview_environment.cpp @@ -85,33 +85,12 @@ namespace flutter_inappwebview_plugin } } - // Ensure COM is initialized before calling CreateCoreWebView2EnvironmentWithOptions - // This is required by WebView2 v140.0.3485.94 and later versions - // Reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/webview2-idl - HRESULT comInitResult = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - bool comWasInitializedByUs = false; - - if (SUCCEEDED(comInitResult)) { - // S_OK means we initialized COM, S_FALSE means it was already initialized - comWasInitializedByUs = (comInitResult == S_OK); - } else if (comInitResult == RPC_E_CHANGED_MODE) { - // COM was already initialized with COINIT_MULTITHREADED - // This is acceptable for WebView2, continue without error - debugLog("COM already initialized with COINIT_MULTITHREADED"); - } else { - // COM initialization failed with some other error - if (completionHandler) { - completionHandler(comInitResult); - } - return; - } - auto hr = CreateCoreWebView2EnvironmentWithOptions( settings && settings->browserExecutableFolder.has_value() ? utf8_to_wide(settings->browserExecutableFolder.value()).c_str() : nullptr, settings && settings->userDataFolder.has_value() ? utf8_to_wide(settings->userDataFolder.value()).c_str() : nullptr, options.Get(), Callback( - [this, hwnd, completionHandler, comWasInitializedByUs](HRESULT result, wil::com_ptr environment) -> HRESULT + [this, hwnd, completionHandler](HRESULT result, wil::com_ptr environment) -> HRESULT { if (succeededOrLog(result)) { environment_ = std::move(environment); @@ -182,23 +161,11 @@ namespace flutter_inappwebview_plugin else if (completionHandler) { completionHandler(result); } - - // Clean up COM if we initialized it - if (comWasInitializedByUs) { - CoUninitialize(); - } - return S_OK; }).Get()); - if (failedAndLog(hr)) { - if (completionHandler) { - completionHandler(hr); - } - // Clean up COM if we initialized it and the call failed immediately - if (comWasInitializedByUs) { - CoUninitialize(); - } + if (failedAndLog(hr) && completionHandler) { + completionHandler(hr); } } From fbebe5a622342dc6eef354a0050cdfc8f2d7abea Mon Sep 17 00:00:00 2001 From: Aleksandr Melnyk Date: Thu, 2 Oct 2025 13:04:51 +0300 Subject: [PATCH 7/8] Revert "fix windows hang-on" This reverts commit c75705b4b3e47ad5d10bf9950d0187aa54eb5952. --- .../custom_platform_view/graphics_context.cc | 14 ------------- .../custom_platform_view/graphics_context.h | 1 - .../in_app_webview/in_app_webview_manager.cpp | 20 ------------------- 3 files changed, 35 deletions(-) diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc index 4d0405892..2bbc1e748 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc @@ -22,20 +22,6 @@ namespace flutter_inappwebview_plugin valid_ = true; } - GraphicsContext::~GraphicsContext() - { - // Explicitly release DirectX resources to prevent hanging process - if (device_context_) { - device_context_->ClearState(); - device_context_->Flush(); - device_context_ = nullptr; - } - - device_winrt_ = nullptr; - device_ = nullptr; - valid_ = false; - } - winrt::com_ptr GraphicsContext::CreateCompositor() { diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h index 6af731840..883bffe71 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h @@ -12,7 +12,6 @@ namespace flutter_inappwebview_plugin class GraphicsContext { public: GraphicsContext(rx::RoHelper* rohelper); - ~GraphicsContext(); inline bool IsValid() const { return valid_; } diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp index 11451f278..8a03dbd33 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp @@ -256,26 +256,6 @@ namespace flutter_inappwebview_plugin keepAliveWebViews.clear(); windowWebViews.clear(); UnregisterClass(windowClass_.lpszClassName, nullptr); - - // Properly cleanup static DirectX resources to prevent hanging process - if (compositor_) { - // Release the extra reference we added in constructor - compositor_->Release(); - compositor_ = nullptr; - } - - // Reset graphics context to release DirectX resources - graphics_context_.reset(); - - // Cleanup dispatcher queue controller - if (dispatcher_queue_controller_) { - dispatcher_queue_controller_ = nullptr; - } - - // Reset RoHelper - rohelper_.reset(); - - valid_ = false; plugin = nullptr; } } From 44710b29bc1f1590bbf084942ef61bc6149f6bcb Mon Sep 17 00:00:00 2001 From: Aleksandr Melnyk Date: Mon, 6 Oct 2025 12:59:25 +0300 Subject: [PATCH 8/8] Reapply "fix windows hang-on" This reverts commit fbebe5a622342dc6eef354a0050cdfc8f2d7abea. --- .../custom_platform_view/graphics_context.cc | 14 +++++++++++++ .../custom_platform_view/graphics_context.h | 1 + .../in_app_webview/in_app_webview_manager.cpp | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc index 2bbc1e748..4d0405892 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.cc @@ -22,6 +22,20 @@ namespace flutter_inappwebview_plugin valid_ = true; } + GraphicsContext::~GraphicsContext() + { + // Explicitly release DirectX resources to prevent hanging process + if (device_context_) { + device_context_->ClearState(); + device_context_->Flush(); + device_context_ = nullptr; + } + + device_winrt_ = nullptr; + device_ = nullptr; + valid_ = false; + } + winrt::com_ptr GraphicsContext::CreateCompositor() { diff --git a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h index 883bffe71..6af731840 100644 --- a/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h +++ b/flutter_inappwebview_windows/windows/custom_platform_view/graphics_context.h @@ -12,6 +12,7 @@ namespace flutter_inappwebview_plugin class GraphicsContext { public: GraphicsContext(rx::RoHelper* rohelper); + ~GraphicsContext(); inline bool IsValid() const { return valid_; } diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp index 8a03dbd33..11451f278 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_manager.cpp @@ -256,6 +256,26 @@ namespace flutter_inappwebview_plugin keepAliveWebViews.clear(); windowWebViews.clear(); UnregisterClass(windowClass_.lpszClassName, nullptr); + + // Properly cleanup static DirectX resources to prevent hanging process + if (compositor_) { + // Release the extra reference we added in constructor + compositor_->Release(); + compositor_ = nullptr; + } + + // Reset graphics context to release DirectX resources + graphics_context_.reset(); + + // Cleanup dispatcher queue controller + if (dispatcher_queue_controller_) { + dispatcher_queue_controller_ = nullptr; + } + + // Reset RoHelper + rohelper_.reset(); + + valid_ = false; plugin = nullptr; } }