From 3c84b942954095be1b4d16510849fe43ac2eb285 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Thu, 18 Nov 2021 10:15:12 -0600 Subject: [PATCH] Handle WM_GETOBJECT while the window is being destroyed (#65) --- platforms/windows/examples/hello_world.rs | 9 ++++++++- platforms/windows/src/tests/mod.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/platforms/windows/examples/hello_world.rs b/platforms/windows/examples/hello_world.rs index 1a86f369d..3b32b9ebb 100644 --- a/platforms/windows/examples/hello_world.rs +++ b/platforms/windows/examples/hello_world.rs @@ -145,7 +145,14 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L LRESULT(0) } WM_GETOBJECT => { - let window_state = unsafe { &*get_window_state(window) }; + let window_state = unsafe { get_window_state(window) }; + if window_state.is_null() { + // We need to be prepared to gracefully handle WM_GETOBJECT + // while the window is being destroyed; this can happen if + // the thread is using a COM STA. + return unsafe { DefWindowProcW(window, message, wparam, lparam) }; + } + let window_state = unsafe { &*window_state }; window_state.manager.handle_wm_getobject(wparam, lparam) } WM_SETFOCUS | WM_EXITMENULOOP | WM_EXITSIZEMOVE => { diff --git a/platforms/windows/src/tests/mod.rs b/platforms/windows/src/tests/mod.rs index df7095f1b..27707f7dc 100644 --- a/platforms/windows/src/tests/mod.rs +++ b/platforms/windows/src/tests/mod.rs @@ -117,7 +117,14 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L LRESULT(0) } WM_GETOBJECT => { - let window_state = unsafe { &*get_window_state(window) }; + let window_state = unsafe { get_window_state(window) }; + if window_state.is_null() { + // We need to be prepared to gracefully handle WM_GETOBJECT + // while the window is being destroyed; this can happen if + // the thread is using a COM STA. + return unsafe { DefWindowProcW(window, message, wparam, lparam) }; + } + let window_state = unsafe { &*window_state }; window_state.manager.handle_wm_getobject(wparam, lparam) } WM_SETFOCUS | WM_EXITMENULOOP | WM_EXITSIZEMOVE => {