diff --git a/.changes/fix-objc-retain-leak.md b/.changes/fix-objc-retain-leak.md new file mode 100644 index 000000000000..d24a6169038a --- /dev/null +++ b/.changes/fix-objc-retain-leak.md @@ -0,0 +1,6 @@ +--- +"tauri": patch:bug +"tauri-runtime-wry": patch:bug +--- + +Avoid leaking Objective-C objects in `WebviewMessage::WithWebview` on Apple targets by replacing `Retained::into_raw` with scoped retained bindings and `Retained::as_ptr` pointer handoff. diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index c48b742af2e7..8a3da8d31fdc 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -3903,26 +3903,24 @@ fn handle_user_message( #[cfg(target_os = "macos")] { use wry::WebViewExtMacOS; + let platform_webview = webview.webview(); + let manager = webview.manager(); + let ns_window = webview.ns_window(); f(Webview { - webview: Retained::into_raw(webview.webview()) as *mut objc2::runtime::AnyObject - as *mut std::ffi::c_void, - manager: Retained::into_raw(webview.manager()) as *mut objc2::runtime::AnyObject - as *mut std::ffi::c_void, - ns_window: Retained::into_raw(webview.ns_window()) as *mut objc2::runtime::AnyObject - as *mut std::ffi::c_void, + webview: Retained::as_ptr(&platform_webview).cast_mut() as *mut std::ffi::c_void, + manager: Retained::as_ptr(&manager).cast_mut() as *mut std::ffi::c_void, + ns_window: Retained::as_ptr(&ns_window).cast_mut() as *mut std::ffi::c_void, }); } #[cfg(target_os = "ios")] { use wry::WebViewExtIOS; + let platform_webview = webview.inner.webview(); + let manager = webview.inner.manager(); f(Webview { - webview: Retained::into_raw(webview.inner.webview()) - as *mut objc2::runtime::AnyObject - as *mut std::ffi::c_void, - manager: Retained::into_raw(webview.inner.manager()) - as *mut objc2::runtime::AnyObject - as *mut std::ffi::c_void, + webview: Retained::as_ptr(&platform_webview).cast_mut() as *mut std::ffi::c_void, + manager: Retained::as_ptr(&manager).cast_mut() as *mut std::ffi::c_void, view_controller: window.ui_view_controller(), }); } diff --git a/crates/tauri-runtime-wry/src/webview.rs b/crates/tauri-runtime-wry/src/webview.rs index 67b607392b88..69a7863a4fd3 100644 --- a/crates/tauri-runtime-wry/src/webview.rs +++ b/crates/tauri-runtime-wry/src/webview.rs @@ -17,6 +17,8 @@ mod imp { mod imp { use std::ffi::c_void; + // These pointers are borrowed from ObjC `Retained` handles owned elsewhere and must + // not be mutated through. TODO: change these to `*const c_void` in v3 (breaking change). pub struct Webview { pub webview: *mut c_void, pub manager: *mut c_void,