Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windows): clone HSTRING so it is not dropped eagerly #1221

Merged
merged 7 commits into from
Apr 24, 2024

Conversation

amrbashir
Copy link
Member

@amrbashir amrbashir commented Apr 15, 2024

@amrbashir amrbashir requested a review from a team as a code owner April 15, 2024 20:46
wusyong
wusyong previously approved these changes Apr 16, 2024
@amrbashir amrbashir marked this pull request as draft April 16, 2024 05:17
Co-authored-by: Crend King <975235+CrendKing@users.noreply.github.com>
@amrbashir amrbashir changed the title fix(windows): move the HSTRING to callback fix(windows): clone HSTRING so it is not dropped eagerly Apr 23, 2024
@CrendKing
Copy link
Contributor

As you mentioned in tauri-apps/tauri#9501, I think you are right that since HString is a ref-counted structure, and PCWSTR is just a pointer and doesn't hold any actual data, the Option<HSTRING> needs to be moved into the closure.

A nitpick for the change: instead of cloning the Option, why not data_directory.as_ref().map(|d| PCWSTR::from_raw(d.as_ptr())) inside the closure? This way 1) the Option is moved in; 2) .as_ref() makes sure it's not dropped prematurely until the end of the closure; 3) cheaper than cloning.

diff --git a/src/webview2/mod.rs b/src/webview2/mod.rs
index 32dd2e2..e0954ac 100644
--- a/src/webview2/mod.rs
+++ b/src/webview2/mod.rs
@@ -248,7 +248,7 @@ impl InnerWebView {
       .as_deref()
       .and_then(|context| context.data_directory())
       .and_then(|path| path.to_str())
-      .map(String::from);
+      .map(HSTRING::from);

     // additional browser args
     let additional_browser_args = pl_attrs.additional_browser_args.unwrap_or_else(|| {
@@ -283,9 +283,6 @@ impl InnerWebView {
     let additional_browser_args = HSTRING::from(additional_browser_args);
     let additional_browser_args = PCWSTR::from_raw(additional_browser_args.as_ptr());

-    let data_directory = data_directory.map(HSTRING::from);
-    let data_directory = data_directory.map(|d| PCWSTR::from_raw(d.as_ptr()));
-
     let (tx, rx) = mpsc::channel();
     CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
       Box::new(move |environmentcreatedhandler| unsafe {
@@ -304,9 +301,11 @@ impl InnerWebView {
         );
         options.SetLanguage(PCWSTR::from_raw(lang.as_ptr()))?;

+        let data_directory_param = data_directory.as_ref().map(|d| PCWSTR::from_raw(d.as_ptr()));
+
         CreateCoreWebView2EnvironmentWithOptions(
           PCWSTR::null(),
-          data_directory.unwrap_or_else(PCWSTR::null),
+          data_directory_param.unwrap_or_else(PCWSTR::null),
           &options,
           &environmentcreatedhandler,
         )

@amrbashir
Copy link
Member Author

thanks, that is better, pushed the change

@wusyong wusyong merged commit c7ca3db into dev Apr 24, 2024
12 checks passed
@wusyong wusyong deleted the fix/windows/data-dir-gibberish branch April 24, 2024 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants