Wayland: six handlers go on using the window after a listener that may have destroyed it (touch, the configure's resize, close) #192

Open
opened 2026-11-17 06:45:38 +00:00 by jorijnvdgraaf · 1 comment

Summary

The rule is written down at interfaces/Crafter.Graphics-Window.cppm:161-163: "Each fires its event last, since a listener may destroy the window, so a backend that makes a second call first checks that the window still exists." PointerListenerHandleEnter follows it (implementations/Crafter.Graphics-Device.cpp:531-532), and so does Win32's WM_CLOSE, which looks its window up again after onClose (implementations/Crafter.Graphics-Window.cpp:174-177). Six Wayland handlers do not: each keeps a local Window* across an Invoke and reads or writes through it afterwards. A listener that destroys its window there leaves the handler on freed memory. Line numbers refer to c006519.

Details

  • Device::touch_down (Device.cpp:734-745): after onTouchDown.Invoke, it reads window->primaryTouchId, writes it and currentMousePos, mouseLeftHeld and lastInputTime, makes the window focusedWindow, and invokes onMouseLeftClick. ~Window clears Device::touchWindow and focusedWindow (Window.cpp:693-698), not the handler's local.
  • Device::touch_up (Device.cpp:759-765): after onTouchUp.Invoke, it reads and writes window->primaryTouchId, mouseLeftHeld and lastInputTime, and invokes onMouseLeftRelease.
  • Device::touch_motion (:781-786): after onTouchMove.Invoke, the same with currentMousePos and onMouseMove.
  • Device::touch_cancel (:797-802): window->onTouchCancel.Invoke() after onMouseLeftRelease.Invoke().
  • Window::xdg_surface_handle_configure (Window.cpp:1927-1934): window->Resize(...) reaches onResize.Invoke() (:771, :792); after it the handler runs wl_surface_commit(window->surface), window->configured = true and window->ApplyToplevelState().
  • Window::xdg_toplevel_handle_close (:1905-1906): window->open = false after onClose.Invoke(). An application that deletes its window from onClose is the likeliest of the six.

Crafter.Event is built for a listener that destroys the event's owner: Invoke returns when it finds deleted set (Crafter.Event.cppm:175 and :215 at 599144c), and ~EventBase sets it (:69). That check reads the destroyed event, so it holds only while the storage has not been used again. It is the event library's own, noted here because the handlers above rely on getting past it.

Found by reading the code, in the pre-push review of 364d210..c006519. Each site is older than that range, which added a write of lastInputTime in the three touch handlers and the ApplyToplevelState call. Not reproduced.

Suggested fix

Where a listener call is followed by more work, find out whether the window still exists before touching it, as the enter handler does: in the touch handlers, go on only while the window is still in Device::windows; in the configure handler, return after Resize when it is not; in the close handler, as Win32 does. Or decide the other way and say so: state in Window.cppm and in AGENTS.consumer.md that a window is not destroyed from its own touch, resize or close listeners.

## Summary The rule is written down at `interfaces/Crafter.Graphics-Window.cppm:161-163`: "Each fires its event last, since a listener may destroy the window, so a backend that makes a second call first checks that the window still exists." `PointerListenerHandleEnter` follows it (`implementations/Crafter.Graphics-Device.cpp:531-532`), and so does Win32's `WM_CLOSE`, which looks its window up again after `onClose` (`implementations/Crafter.Graphics-Window.cpp:174-177`). Six Wayland handlers do not: each keeps a local `Window*` across an `Invoke` and reads or writes through it afterwards. A listener that destroys its window there leaves the handler on freed memory. Line numbers refer to `c006519`. ## Details - `Device::touch_down` (`Device.cpp:734-745`): after `onTouchDown.Invoke`, it reads `window->primaryTouchId`, writes it and `currentMousePos`, `mouseLeftHeld` and `lastInputTime`, makes the window `focusedWindow`, and invokes `onMouseLeftClick`. `~Window` clears `Device::touchWindow` and `focusedWindow` (`Window.cpp:693-698`), not the handler's local. - `Device::touch_up` (`Device.cpp:759-765`): after `onTouchUp.Invoke`, it reads and writes `window->primaryTouchId`, `mouseLeftHeld` and `lastInputTime`, and invokes `onMouseLeftRelease`. - `Device::touch_motion` (`:781-786`): after `onTouchMove.Invoke`, the same with `currentMousePos` and `onMouseMove`. - `Device::touch_cancel` (`:797-802`): `window->onTouchCancel.Invoke()` after `onMouseLeftRelease.Invoke()`. - `Window::xdg_surface_handle_configure` (`Window.cpp:1927-1934`): `window->Resize(...)` reaches `onResize.Invoke()` (`:771`, `:792`); after it the handler runs `wl_surface_commit(window->surface)`, `window->configured = true` and `window->ApplyToplevelState()`. - `Window::xdg_toplevel_handle_close` (`:1905-1906`): `window->open = false` after `onClose.Invoke()`. An application that deletes its window from `onClose` is the likeliest of the six. Crafter.Event is built for a listener that destroys the event's owner: `Invoke` returns when it finds `deleted` set (`Crafter.Event.cppm:175` and `:215` at 599144c), and `~EventBase` sets it (`:69`). That check reads the destroyed event, so it holds only while the storage has not been used again. It is the event library's own, noted here because the handlers above rely on getting past it. Found by reading the code, in the pre-push review of 364d210..c006519. Each site is older than that range, which added a write of `lastInputTime` in the three touch handlers and the `ApplyToplevelState` call. Not reproduced. ## Suggested fix Where a listener call is followed by more work, find out whether the window still exists before touching it, as the enter handler does: in the touch handlers, go on only while the window is still in `Device::windows`; in the configure handler, return after `Resize` when it is not; in the close handler, as Win32 does. Or decide the other way and say so: state in `Window.cppm` and in `AGENTS.consumer.md` that a window is not destroyed from its own touch, resize or close listeners.
Author
Owner

Two more handlers, the keyboard's, with a shape of their own: they keep no local Window*, but they call through Device::keyboardWindow again after an Invoke, and a listener can have left that static null. Line numbers refer to 148ed01.

  • Device::keyboard_key checks keyboardWindow once, on entry (implementations/Crafter.Graphics-Device.cpp:757), invokes onRawKeyHold or onRawKeyDown through it (:761, :764), and then, for a key that brings text, calls keyboardWindow->onTextInput.Invoke(utf8) (:776).
  • Device::TickKeyRepeats checks it on entry (:819), then calls onRawKeyDown, onRawKeyHold and onTextInput through it for each repeat (:844-847).

A listener nulls it by destroying its window (~Window clears it, Window.cpp:700), or by dispatching Wayland events when the dispatch brings a wl_keyboard.leave (keyboard_leave sets it to null, Device.cpp:738). Clipboard::GetText dispatches (Clipboard.cpp:341, :368), and so does a Window's constructor (Window.cpp:393). The next call through the static then dereferences null. A dispatch that brings a leave and then an enter for another of our windows sends the rest of the key to that window instead.

A key that brings no text (Backspace, the arrows) skips :776, and after a leave inside its listeners it has a second effect: keyboard_leave stops the repeat (:739), and keyboard_key then arms it again for that key (:781-786). TickKeyRepeats does nothing while keyboardWindow is null, but the next enter sets it, and the key repeats into the window that has the keyboard then until a key is pressed there or the keyboard leaves again.

Found by reading the code, in the pre-push review of #167's fix (594e161..148ed01). The range left these dereferences as they were; it only narrowed :776, which a key no longer reaches while no keymap has loaded, since such a key brings no text. Not reproduced.

The fix suggested above fits both: after each Invoke, go on only while keyboardWindow is still the window the handler started with, and arm the repeat only then.

Two more handlers, the keyboard's, with a shape of their own: they keep no local `Window*`, but they call through `Device::keyboardWindow` again after an `Invoke`, and a listener can have left that static null. Line numbers refer to `148ed01`. - `Device::keyboard_key` checks `keyboardWindow` once, on entry (`implementations/Crafter.Graphics-Device.cpp:757`), invokes `onRawKeyHold` or `onRawKeyDown` through it (`:761`, `:764`), and then, for a key that brings text, calls `keyboardWindow->onTextInput.Invoke(utf8)` (`:776`). - `Device::TickKeyRepeats` checks it on entry (`:819`), then calls `onRawKeyDown`, `onRawKeyHold` and `onTextInput` through it for each repeat (`:844-847`). A listener nulls it by destroying its window (`~Window` clears it, `Window.cpp:700`), or by dispatching Wayland events when the dispatch brings a `wl_keyboard.leave` (`keyboard_leave` sets it to null, `Device.cpp:738`). `Clipboard::GetText` dispatches (`Clipboard.cpp:341`, `:368`), and so does a `Window`'s constructor (`Window.cpp:393`). The next call through the static then dereferences null. A dispatch that brings a leave and then an enter for another of our windows sends the rest of the key to that window instead. A key that brings no text (Backspace, the arrows) skips `:776`, and after a leave inside its listeners it has a second effect: `keyboard_leave` stops the repeat (`:739`), and `keyboard_key` then arms it again for that key (`:781-786`). `TickKeyRepeats` does nothing while `keyboardWindow` is null, but the next enter sets it, and the key repeats into the window that has the keyboard then until a key is pressed there or the keyboard leaves again. Found by reading the code, in the pre-push review of #167's fix (`594e161..148ed01`). The range left these dereferences as they were; it only narrowed `:776`, which a key no longer reaches while no keymap has loaded, since such a key brings no text. Not reproduced. The fix suggested above fits both: after each `Invoke`, go on only while `keyboardWindow` is still the window the handler started with, and arm the repeat only then.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#192
No description provided.