From 484506e3cd39c6e5551eed440fbb25a174791cca Mon Sep 17 00:00:00 2001 From: Fatih Uzunoglu <fuzun54@outlook.com> Date: Mon, 3 Mar 2025 01:34:40 +0200 Subject: [PATCH] qt: do not create platform window in `WinId()` in mainctx_win32.cpp Previously the window handle was retrieved through private platform interface. It was recently changed to use `QWindow::winId()` instead, as it is public and more convenient to use. However, `QWindow::winId()` creates the platform window if it does not exist. This is normally not a problem, but since `WinId()` is may be called within the native event handler, it is a problem. I did not realize that `WinId()` was called at inappropriate times (either during destruction of the platform window when the window is destroyed, or after that as we don't want to re-create the platform window when closing the interface), that's why I did not pay attention on having a check here. Now that the problem is obvious, we should fix this mistake made in 7e063c67. This is trivial to fix, we can simply check if the pointer returned by either `QWindow::handle()` or `QSurface::surfaceHandle()` is a null pointer, and return early if it is before calling `QWindow::winId()`. As a side note, I believe that having a const method named as `winId()` which mutates the window by creating platform window if it does not exist is a really bad idea. I really wonder why Qt behaves like that, especially considering there is already `QWindow::create()` that creates the platform window. --- modules/gui/qt/maininterface/mainctx_win32.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/gui/qt/maininterface/mainctx_win32.cpp b/modules/gui/qt/maininterface/mainctx_win32.cpp index 4d722959c1d..9fa9b5d9c6c 100644 --- a/modules/gui/qt/maininterface/mainctx_win32.cpp +++ b/modules/gui/qt/maininterface/mainctx_win32.cpp @@ -107,6 +107,8 @@ namespace { HWND WinId( const QWindow *window ) { assert(window); + if (!window->handle()) + return NULL; return reinterpret_cast<HWND>(window->winId()); } -- GitLab