Skip to content

Commit ba865f2

Browse files
fix(windows): dpi getter leaks HDC handles (#15614)
1 parent 0299da0 commit ba865f2

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

.changes/fix-dpi-leak-hdc.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri-runtime-wry: patch:bug
3+
---
4+
5+
Fix getting the DPI internally leaks `HDC` handles on Windows. This also improves the resizing speed on undecorated windows

crates/tauri-runtime-wry/src/util.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ mod imp {
6868

6969
#[allow(non_snake_case)]
7070
pub unsafe fn hwnd_dpi(hwnd: HWND) -> u32 {
71-
let hdc = GetDC(Some(hwnd));
72-
if hdc.is_invalid() {
73-
return USER_DEFAULT_SCREEN_DPI;
74-
}
7571
if let Some(GetDpiForWindow) = *GET_DPI_FOR_WINDOW {
7672
// We are on Windows 10 Anniversary Update (1607) or later.
7773
match GetDpiForWindow(hwnd) {
@@ -95,9 +91,15 @@ mod imp {
9591
} else {
9692
// We are on Vista or later.
9793
if IsProcessDPIAware().as_bool() {
94+
let hdc = GetDC(Some(hwnd));
95+
if hdc.is_invalid() {
96+
return USER_DEFAULT_SCREEN_DPI;
97+
}
9898
// If the process is DPI aware, then scaling must be handled by the application using
9999
// this DPI value.
100-
GetDeviceCaps(Some(hdc), LOGPIXELSX) as u32
100+
let dpi = GetDeviceCaps(Some(hdc), LOGPIXELSX) as u32;
101+
ReleaseDC(Some(hwnd), hdc);
102+
dpi
101103
} else {
102104
// If the process is DPI unaware, then scaling is performed by the OS; we thus return
103105
// 96 (scale factor 1.0) to prevent the window from being re-scaled by both the

0 commit comments

Comments
 (0)