From 075a1fd1d67c6e347ab40f460819067b947f92f6 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <zhaoyuhang@rankyee.com> Date: 周三, 06 12月 2023 18:06:54 +0800 Subject: [PATCH] fix mem leak --- src/core/contexts/win32windowcontext.cpp | 22 ++++++++++++++-------- 1 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp index 83e2cee..a843e90 100644 --- a/src/core/contexts/win32windowcontext.cpp +++ b/src/core/contexts/win32windowcontext.cpp @@ -203,7 +203,7 @@ return 0; } const DynamicApis &apis = DynamicApis::instance(); - if (apis.pGetDpiForWindow) { // Win10 + if (apis.pGetDpiForWindow) { // Win10 return apis.pGetDpiForWindow(hwnd); } else if (apis.pGetDpiForMonitor) { // Win8.1 HMONITOR monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); @@ -308,7 +308,8 @@ ::GetWindowRect(hwnd, &windowRect); const auto newX = (RECT_WIDTH(monitorInfo.rcMonitor) - RECT_WIDTH(windowRect)) / 2; const auto newY = (RECT_HEIGHT(monitorInfo.rcMonitor) - RECT_HEIGHT(windowRect)) / 2; - ::SetWindowPos(hwnd, nullptr, newX, newY, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER); + ::SetWindowPos(hwnd, nullptr, newX, newY, 0, 0, + SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER); } static inline bool isFullScreen(HWND hwnd) { @@ -442,11 +443,17 @@ static WindowsNativeEventFilter *instance; static inline void install() { + if (instance) { + return; + } instance = new WindowsNativeEventFilter(); installNativeEventFilter(instance); } static inline void uninstall() { + if (!instance) { + return; + } removeNativeEventFilter(instance); delete instance; instance = nullptr; @@ -538,7 +545,7 @@ return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam); } - Win32WindowContext::Win32WindowContext(QObject *host, WindowItemDelegate *delegate) + Win32WindowContext::Win32WindowContext(const QObject *host, const WindowItemDelegate *delegate) : AbstractWindowContext(host, delegate) { } @@ -575,9 +582,7 @@ ::SetWindowLongPtrW(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(QWKHookedWndProc)); // Install global native event filter - if (!WindowsNativeEventFilter::instance) { - WindowsNativeEventFilter::install(); - } + WindowsNativeEventFilter::install(); // Cache window ID windowId = winId; @@ -908,7 +913,8 @@ case WM_SHOWWINDOW: { if (!centered) { // If wParam is TRUE, the window is being shown. - // If lParam is zero, the message was sent because of a call to the ShowWindow function. + // If lParam is zero, the message was sent because of a call to the ShowWindow + // function. if (wParam && lParam == 0) { centered = true; moveToDesktopCenter(hWnd); @@ -1004,7 +1010,7 @@ // and align it with the upper-left corner of our new client area". const auto clientRect = wParam ? &(reinterpret_cast<LPNCCALCSIZE_PARAMS>(lParam))->rgrc[0] - : reinterpret_cast<LPRECT>(lParam); + : reinterpret_cast<LPRECT>(lParam); if (isWin10OrGreater()) { // Store the original top margin before the default window procedure applies the // default frame. -- Gitblit v1.9.1