From b0c493a4ea7d69a19f88e5b89cbb111a7c006517 Mon Sep 17 00:00:00 2001 From: Kirill Gribunin Date: Wed, 24 Sep 2025 20:57:05 +0200 Subject: [PATCH] FIX: Fixed GDI object leak when a resizable window is created and then closed on Windows platform (#14209) Co-authored-by: Kirill Gribunin --- .../tauri-runtime-wry/src/undecorated_resizing.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/tauri-runtime-wry/src/undecorated_resizing.rs b/crates/tauri-runtime-wry/src/undecorated_resizing.rs index 36f54f69a..66a98710c 100644 --- a/crates/tauri-runtime-wry/src/undecorated_resizing.rs +++ b/crates/tauri-runtime-wry/src/undecorated_resizing.rs @@ -420,17 +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); - CombineRgn(Some(hrgn1), Some(hrgn1), Some(hrgn2), RGN_DIFF); + // Wrap hrgn2 in Owned so it is automatically freed when going out of scope + let hrgn2 = Owned::new(CreateRectRgn(x1, y1, x2, y2)); - SetWindowRgn(hwnd, Some(hrgn1), true); + CombineRgn(Some(hrgn1), Some(hrgn1), Some(*hrgn2), RGN_DIFF); + + // 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) {