diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index b05977da53..2bc0b2aa72 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -476,6 +476,10 @@ r#"Object.defineProperty(window, 'ipc', { let ns_window = window.ns_window() as id; let _: () = msg_send![ns_window, setContentView: webview]; + // To prevent memory leak, we need to remove webview by script message handler. + let interop = NSString::new("interop"); + let _: () = msg_send![manager, addScriptMessageHandler:webview name:interop]; + // make sure the window is always on top when we create a new webview let app_class = class!(NSApplication); let app: id = msg_send![app_class, sharedApplication]; @@ -650,6 +654,8 @@ impl Drop for InnerWebView { // So we need to override the process by navigating to `about:blank`. self.navigate("about:blank"); + let _: () = msg_send![self.manager, removeAllScriptMessageHandlers]; + let _: Id<_> = Id::from_ptr(self.webview); #[cfg(target_os = "macos")] let _: Id<_> = Id::from_ptr(self.ns_window);