Repository navigation
win32: fix crash when moving a window that owns non-winit windows - #4700
Merged
ogoffart merged 1 commit intoSep 19, 2026
Merged
Conversation
wilsonglasser
pushed a commit
to wilsonglasser/winit
that referenced
this pull request
Sep 27, 2026
…st-windowing#4700) This fixes a crash introduced by rust-windowing#4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop. `reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`: ```rs let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData; if !userdata_ptr.is_null() { let userdata = unsafe { &*userdata_ptr }; ``` Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries. In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time: - `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc` - followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change. To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not. Tested on Windows 11 25H2 (build 26200.9445).
2 of 4 tasks
kchibisov
pushed a commit
that referenced
this pull request
Oct 24, 2026
) This fixes a crash introduced by #4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop. `reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`: ```rs let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData; if !userdata_ptr.is_null() { let userdata = unsafe { &*userdata_ptr }; ``` Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries. In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time: - `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc` - followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change. To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not. Tested on Windows 11 25H2 (build 26200.9445).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes a crash introduced by #4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, Graphite, which freezes the app on the first move and followed shortly by a crash to desktop.
reposition_owned_windowsruns on everyWM_WINDOWPOSCHANGEDthat moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one'sGWL_USERDATA, and dereferences the value as*mut WindowData:Nothing checks that the enumerated window was created by winit.
GWL_USERDATAis free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself throughraw-window-handle, and potentially dialogs or controls created on the same thread by other libraries.In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's
HWNDinGWLP_USERDATA. Moving the winit window therefore crashes on the first position change, every time:0xC0000005access violation inwinit_win32::event_loop::reposition_owned_windows::enum_proc0xC000041D(STATUS_FATAL_USER_CALLBACK_EXCEPTION), since the fault happens inside the window procedure while Windows is dispatching the messageThis PR fixes it by comparing the window's
GWL_WNDPROCagainstpublic_window_callbackbefore touchingGWL_USERDATAand skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is aWindowData. Anchored popups still follow their owner with this change.To reproduce outside of Graphite, on winit's
master, in any of its examples: create a top-level Win32 window (e.g.WS_POPUP) withCreateWindowExW, passing the winit window'sHWNDas the owner, callSetWindowLongPtrW(helper, GWLP_USERDATA, 1), then move the winit window by dragging it or callingset_outer_position. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the&*userdata_ptrline, and a release build exits with0xC000041D. With this PR it does not.Tested on Windows 11 25H2 (build 26200.9445).
changelogmodule if knowledge of this change could be valuable to usersUpdated documentation to reflect any user-facing changes, including notes of platform-specific behaviorCreated or updated an example program if it would help users understand this functionality