From 34d7cf770605f0523f43bfc0e1bfb8d37388394f Mon Sep 17 00:00:00 2001 From: Kirill Gribunin Date: Tue, 23 Sep 2025 08:18:29 -0400 Subject: [PATCH 1/3] Fixed GDI object leak when a window is created and then closed --- crates/tauri-runtime-wry/src/undecorated_resizing.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/tauri-runtime-wry/src/undecorated_resizing.rs b/crates/tauri-runtime-wry/src/undecorated_resizing.rs index 36f54f69a6d0..5676eb8c8ea6 100644 --- a/crates/tauri-runtime-wry/src/undecorated_resizing.rs +++ b/crates/tauri-runtime-wry/src/undecorated_resizing.rs @@ -430,6 +430,7 @@ mod windows { CombineRgn(Some(hrgn1), Some(hrgn1), Some(hrgn2), RGN_DIFF); + DeleteObject(hrgn2.into()); SetWindowRgn(hwnd, Some(hrgn1), true); } From a35c84198b0cf8a64c27e3edbec0cac1a3d1d41a Mon Sep 17 00:00:00 2001 From: Kirill Gribunin Date: Wed, 24 Sep 2025 14:29:50 -0400 Subject: [PATCH 2/3] Better HRGN variable release in set_drag_hwnd_rgn --- .../src/undecorated_resizing.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/tauri-runtime-wry/src/undecorated_resizing.rs b/crates/tauri-runtime-wry/src/undecorated_resizing.rs index 5676eb8c8ea6..4848a43ed692 100644 --- a/crates/tauri-runtime-wry/src/undecorated_resizing.rs +++ b/crates/tauri-runtime-wry/src/undecorated_resizing.rs @@ -420,18 +420,24 @@ mod windows { let border_x = util::get_system_metrics_for_dpi(SM_CXFRAME, dpi); let border_y = util::get_system_metrics_for_dpi(SM_CYFRAME, dpi); - let hrgn1 = CreateRectRgn(0, 0, width, height); + // hrgn1 must be mutable to call .free() later + let mut hrgn1 = CreateRectRgn(0, 0, width, height); let x1 = if only_top { 0 } else { border_x }; let y1 = border_y; let x2 = if only_top { width } else { width - border_x }; let y2 = if only_top { height } else { height - border_y }; - let hrgn2 = CreateRectRgn(x1, y1, x2, y2); + + // Wrap hrgn2 in Owned so it is automatically freed when going out of scope + let hrgn2 = Owned::new(CreateRectRgn(x1, y1, x2, y2)); - CombineRgn(Some(hrgn1), Some(hrgn1), Some(hrgn2), RGN_DIFF); + CombineRgn(Some(hrgn1), Some(hrgn1), Some(*hrgn2), RGN_DIFF); - DeleteObject(hrgn2.into()); - SetWindowRgn(hwnd, Some(hrgn1), true); + // Try to set the window region + if SetWindowRgn(hwnd, Some(hrgn1), true) == 0 { + // If it fails, we must free hrgn1 manually + hrgn1.free(); + } } pub fn update_drag_hwnd_rgn_for_undecorated(hwnd: isize, has_undecorated_shadows: bool) { From f85130797b86753ea0e62e84793e824c22d8b7e1 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 24 Sep 2025 21:45:15 +0300 Subject: [PATCH 3/3] fmt --- crates/tauri-runtime-wry/src/undecorated_resizing.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/tauri-runtime-wry/src/undecorated_resizing.rs b/crates/tauri-runtime-wry/src/undecorated_resizing.rs index 4848a43ed692..66a98710cbbb 100644 --- a/crates/tauri-runtime-wry/src/undecorated_resizing.rs +++ b/crates/tauri-runtime-wry/src/undecorated_resizing.rs @@ -427,7 +427,7 @@ mod windows { let y1 = border_y; let x2 = if only_top { width } else { width - border_x }; let y2 = if only_top { height } else { height - border_y }; - + // Wrap hrgn2 in Owned so it is automatically freed when going out of scope let hrgn2 = Owned::new(CreateRectRgn(x1, y1, x2, y2)); @@ -435,8 +435,8 @@ mod windows { // Try to set the window region if SetWindowRgn(hwnd, Some(hrgn1), true) == 0 { - // If it fails, we must free hrgn1 manually - hrgn1.free(); + // If it fails, we must free hrgn1 manually + hrgn1.free(); } }