Skip to content

Add some safety for webview window on windows and fix for mac#451

Merged
boyan01 merged 3 commits into
MixinNetwork:mainfrom
paulanatoleclaudot-betclic:fix/safer_desktop_window_webview
Nov 11, 2025
Merged

Add some safety for webview window on windows and fix for mac#451
boyan01 merged 3 commits into
MixinNetwork:mainfrom
paulanatoleclaudot-betclic:fix/safer_desktop_window_webview

Conversation

@paulanatoleclaudot-betclic
Copy link
Copy Markdown
Contributor

Fix memory leaks, crashes, and bugs in desktop_webview_window

Summary

This PR addresses critical memory leaks, potential crashes, and bugs across Windows and macOS implementations of the desktop_webview_window package. The changes improve stability, prevent memory leaks, and fix incorrect behavior.

Changes

Commit 1: Fix critical memory leaks and crashes in Windows webview implementation

File: packages/desktop_webview_window/windows/web_view.cc

Issues Fixed:

  • Memory leak in User Agent retrieval: Fixed incorrect array declaration LPWSTR user_agent[256] and memory leak by using wil::unique_cotaskmem_string
  • Memory leak in Settings interface: Changed raw pointer ICoreWebView2Settings *settings to wil::com_ptr for automatic COM reference management
  • Memory leaks in URI handling: Fixed leaks in NewWindowRequested and NavigationStarting handlers by using wil::unique_cotaskmem_string
  • Memory leaks in cookie operations: Fixed leaks in GetAllCookies method for all cookie string fields (name, value, domain, path)
  • Missing HRESULT error checking: Added proper error checking for get_CoreWebView2(), get_Settings(), and all cookie operations to prevent crashes
  • Type safety issue: Added type checking with std::holds_alternative<bool>() before accessing variant value to prevent exceptions
  • Null pointer dereference: Added null check for ExecuteScript result to prevent crashes

Technical Details:

  • Replaced all raw COM interface pointers with wil::com_ptr for automatic lifetime management
  • Replaced all LPWSTR allocations with wil::unique_cotaskmem_string for automatic memory cleanup
  • Added comprehensive HRESULT error checking throughout
  • Improved error handling in async callbacks

Impact: Prevents memory leaks that could accumulate over time and crashes from invalid COM objects or null pointers.


Commit 2: Add removeAllWebMessageReceivedCallback method and improve code formatting

Files:

  • packages/desktop_webview_window/lib/src/webview.dart
  • packages/desktop_webview_window/lib/src/webview_impl.dart

Changes:

  • Added removeAllWebMessageReceivedCallback() method to clear all web message callbacks at once
  • Improved code formatting for better readability (consistent parameter formatting)
  • Updated method signatures to follow Dart style guidelines

Impact: Provides better control over web message callbacks and improves code maintainability.


Commit 3: Fix macOS JavaScript interface removal bug

File: packages/desktop_webview_window/macos/Classes/DesktopWebviewWindowPlugin.swift

Issue Fixed:

  • Fixed incorrect method call in unregisterJavaScriptMessageHandler - was calling addJavascriptInterface instead of removeJavascriptInterface

Impact: JavaScript interface removal now works correctly on macOS, preventing potential memory leaks and incorrect behavior.


Testing

  • Verified memory leaks are fixed (no COM interface leaks, no string memory leaks)
  • Verified error handling prevents crashes
  • Verified cookie operations work correctly
  • Verified JavaScript interface removal works on macOS
  • Verified web message callbacks can be cleared properly

Breaking Changes

None - all changes are bug fixes and improvements that maintain backward compatibility.

Related Issues

Fixes memory leaks and crashes that could occur during:

  • WebView initialization
  • Navigation events
  • Cookie retrieval operations
  • JavaScript interface management on macOS

@boyan01
Copy link
Copy Markdown
Contributor

boyan01 commented Nov 11, 2025

Thanks for the contribution. LGTM

@boyan01 boyan01 merged commit bf79d2f into MixinNetwork:main Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants