Skip to content

fix(windows): resolve GDI leaks in Settings UI and System Tray - #394

Open
so-sai wants to merge 3 commits into
khaphanspace:feat/windows-platformfrom
so-sai:feat/windows-platform
Open

fix(windows): resolve GDI leaks in Settings UI and System Tray#394
so-sai wants to merge 3 commits into
khaphanspace:feat/windows-platformfrom
so-sai:feat/windows-platform

Conversation

@so-sai

@so-sai so-sai commented Jun 19, 2026

Copy link
Copy Markdown
  1. Rò rỉ font handle (HFONT) trong SettingsWindow
    Vị trí: platforms/windows/src/settings_window.cpp:141
    Vấn đề: hFont được tạo cục bộ trong CreateControls() và gán cho các control tĩnh thông qua WM_SETFONT nhưng không bao giờ giải phóng bằng DeleteObject. Khi mở lại màn hình settings hoặc khi đổi DPI, font mới được tạo liên tục gây tích tụ leak.
    Đề xuất: Đưa hFont_ làm member của lớp SettingsWindow để giải phóng khi hủy đối tượng hoặc trước khi tạo font mới (khi đổi DPI).
    diff

diff --git a/platforms/windows/src/settings_window.h b/platforms/windows/src/settings_window.h
index 52f2dcd..bba0766 100644
--- a/platforms/windows/src/settings_window.h
+++ b/platforms/windows/src/settings_window.h
@@ -34,6 +34,7 @@ private:
HWND hwnd_ = nullptr;
HWND cmbMethod_ = nullptr;
bool visible_ = false;

  • HFONT hFont_ = nullptr;

    // Custom painted section positions
    diff

diff --git a/platforms/windows/src/settings_window.cpp b/platforms/windows/src/settings_window.cpp
index b77255a..702eab9 100644
--- a/platforms/windows/src/settings_window.cpp
+++ b/platforms/windows/src/settings_window.cpp
@@ -48,6 +48,7 @@ SettingsWindow& SettingsWindow::Instance() {
}

SettingsWindow::~SettingsWindow() {

  • if (hFont_) { DeleteObject(hFont_); }
    if (hwnd_) {
    DestroyWindow(hwnd_);
    }
    @@ -138,7 +139,8 @@ void SettingsWindow::CreateControls() {

    // Create DPI-scaled font
    int fontSize = Scale(13, dpi);

  • HFONT hFont = CreateFontW(-fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  • if (hFont_) { DeleteObject(hFont_); }
  • hFont_ = CreateFontW(-fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
    CLEARTYPE_QUALITY, DEFAULT_PITCH, L"Segoe UI");

@@ -165,7 +167,7 @@ void SettingsWindow::CreateControls() {
WS_CHILD | WS_VISIBLE | SS_LEFT | SS_CENTERIMAGE,
labelX, y, labelWidth, rowHeight,
hwnd_, NULL, hInst, NULL);

  •    SendMessage(lbl, WM_SETFONT, (WPARAM)hFont, TRUE);
    
  •    SendMessage(lbl, WM_SETFONT, (WPARAM)hFont_, TRUE);
    
  1. Rò rỉ icon handle (HICON) trong SystemTray
    Vị trí: platforms/windows/src/system_tray.cpp:34,59
    Vấn đề: Icon được load qua LoadImageW trả về một handle riêng biệt cần được thu hồi bằng DestroyIcon. Hàm Destroy() hiện tại chỉ xóa khay hệ thống (NIM_DELETE) mà chưa hủy handle của icon này.
    Đề xuất: Gọi DestroyIcon(nid_.hIcon) khi giải phóng SystemTray.
    diff

diff --git a/platforms/windows/src/system_tray.cpp b/platforms/windows/src/system_tray.cpp
index 85e7a4d..029920a 100644
--- a/platforms/windows/src/system_tray.cpp
+++ b/platforms/windows/src/system_tray.cpp
@@ -57,6 +57,7 @@ bool SystemTray::Create(HWND hwnd) {
void SystemTray::Destroy() {
if (created_) {
Shell_NotifyIconW(NIM_DELETE, &nid_);

  •    if (nid_.hIcon) { DestroyIcon(nid_.hIcon); nid_.hIcon = nullptr; }
       created_ = false;
    
    }
    }

so-sai added 2 commits June 19, 2026 11:08
- Store HFONT as member and call DeleteObject to prevent GDI leak on UI refresh
- Call DestroyIcon in SystemTray destructor to clean up handles
- Thêm cờ /utf-8 vào CMake để MSVC biên dịch đúng string UTF-8
- Thêm pragma code_page(65001) vào resources.rc để fix lỗi font trên Title Bar
@so-sai

so-sai commented Jun 19, 2026

Copy link
Copy Markdown
Author

Thêm cờ /utf-8 trong CMake: Giúp trình biên dịch MSVC diễn giải chính xác các chuỗi literal UTF-8 trong code C++.

  • Thêm chỉ thị #pragma code_page(65001) trong resources.rc: Khắc phục triệt để lỗi hiển thị sai các ký tự tiếng Việt có dấu trên thanh Tiêu đề (Title bar) của các cửa sổ.

@so-sai

so-sai commented Jun 19, 2026

Copy link
Copy Markdown
Author

-Tối ưu hóa logic bắt phím Shift bằng phép XOR (capsLock ^ shift):
Nhận diện đúng "Effective Case" (viết hoa/viết thường) cần thiết cho Rust engine xử lý. Giải pháp này giúp loại bỏ rủi ro kẹt phím Shift khi người dùng chuyển sang màn hình UAC (Secure Desktop) nếu theo dõi sự kiện keyup thủ công.

  • Thêm Zalo (zalo.exe) vào Slow Injection List:
    Khắc phục lỗi race condition về thời gian phản hồi (Timing Race) trên ứng dụng Electron của Zalo, đảm bảo gõ tổ hợp như Shift + d + d ra chữ Đ ổn định 100% trên cả Zalo và các ứng dụng Native/Notepad.

…sing keycode mappings

- keyboard_hook.cpp: XOR CapsLock with Shift for correct case (Shift+dd → Đ)
- app_compat.cpp: Add zalo.exe to slow injection list
- keyboard_hook.cpp: Add missing VkToMacKeycode mappings for period (0x2F),
  comma (0x2B), slash (0x2C), and number 1 (0x12) so Rust engine receives
  sentence-ending punctuation (. ! ?) and auto-capitalize works correctly
@so-sai
so-sai force-pushed the feat/windows-platform branch from e971d67 to 10cf3f5 Compare June 19, 2026 10:20
@so-sai

so-sai commented Jun 19, 2026

Copy link
Copy Markdown
Author

Sửa lỗi tính năng Tự động viết hoa (Auto-Capitalize) không hoạt động:
Hàm VkToMacKeycode thiếu mapping cho các phím dấu câu (. , ? !), khiến chúng bị coi là phím không xác định (0xFF) và bị lọc bỏ, không bao giờ gửi xuống Rust engine. Mình đã bổ sung đủ mapping cho VK_OEM_PERIOD (.), VK_OEM_COMMA (,), VK_OEM_2 (?), và '1' (!) sang keycode tương ứng của macOS để engine có thể nhận biết và tự động viết hoa sau dấu câu chính xác.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant