Keavon Chambers a0c5d0a96b win32: fix crash when moving a window that owns non-winit windows (#4700)
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).
2026-09-19 07:48:56 +02:00
2024-07-28 17:38:44 +02:00
2026-09-05 03:43:23 +09:00
2024-07-27 18:26:52 +02:00
2025-06-07 13:07:12 +09:00
2024-03-07 17:32:06 +04:00
2026-09-02 21:11:01 +09:00
2014-07-27 11:41:26 +02:00
2026-09-05 03:43:23 +09:00
2024-07-28 17:38:44 +02:00
2024-07-28 17:38:44 +02:00
2026-07-16 14:48:12 +02:00

winit - Cross-platform window creation and management in Rust

Crates.io Docs.rs UNSTABLE docs CI Status

[dependencies]
winit = "0.31.0-beta.3"

Documentation

For features within the scope of winit, see FEATURES.md.

For features outside the scope of winit, see Are we GUI Yet? and Are we game yet?, depending on what kind of project you're looking to do.

Contact Us

Join us in our Matrix room.

The maintainers have a meeting every friday at UTC 15. The meeting notes can be found here.

Usage

Winit is a window creation and management library. It can create windows and lets you handle events (for example: the window being resized, a key being pressed, a mouse movement, etc.) produced by the window.

Winit is designed to be a low-level brick in a hierarchy of libraries. Consequently, in order to show something on the window you need to use the platform-specific getters provided by winit, or another library.

CONTRIBUTING

For contributing guidelines see CONTRIBUTING.md.

MSRV Policy

This crate's Minimum Supported Rust Version (MSRV) is 1.86. Changes to the MSRV will be accompanied by a minor version bump.

As a tentative policy, the upper bound of the MSRV is given by the following formula:

min(sid, stable - 3)

Where sid is the current version of rustc provided by Debian Sid, and stable is the latest stable version of Rust. This bound may be broken in case of a major ecosystem shift or a security vulnerability.

An exception is made for the Android platform, where a higher Rust version must be used for certain Android features. In this case, the MSRV will be capped at the latest stable version of Rust minus three. This inconsistency is not reflected in Cargo metadata, as it is not powerful enough to expose this restriction.

Redox OS is also not covered by this MSRV policy, as it requires a Rust nightly toolchain to compile.

All crates in the rust-windowing organizations have the same MSRV policy.

Platform-specific usage

Check out the winit::platform module for platform-specific usage.

Repository License

Note that the license in LICENSE doesn't apply in full to the DPI package ./dpi. Full details can be found in that folder's README.

S
Description
Window handling library in pure Rust
Readme Apache-2.0
17 MiB
0 Stars 1 Watchers 0 Forks
Languages
Rust 100%