Skip to content

win32: fix crash when moving a window that owns non-winit windows - #4700

Merged
ogoffart merged 1 commit into
rust-windowing:masterfrom
Keavon:fix-win32-owned-window-crash
Sep 19, 2026
Merged

ogoffart merged 1 commit into
rust-windowing:masterfrom
Keavon:fix-win32-owned-window-crash

Conversation

@Keavon

@Keavon Keavon commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

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_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:

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).

  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality

@ogoffart ogoffart left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@ogoffart
ogoffart merged commit a0c5d0a into rust-windowing:master Sep 19, 2026
53 checks passed
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).
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants