Wayland: six handlers go on using the window after a listener that may have destroyed it (touch, the configure's resize, close) #192
Labels
No labels
claude:blocked
claude:done
claude:failed
claude:in-progress
claude:ready
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Catcrafts/Crafter.Graphics#192
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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."PointerListenerHandleEnterfollows it (implementations/Crafter.Graphics-Device.cpp:531-532), and so does Win32'sWM_CLOSE, which looks its window up again afteronClose(implementations/Crafter.Graphics-Window.cpp:174-177). Six Wayland handlers do not: each keeps a localWindow*across anInvokeand reads or writes through it afterwards. A listener that destroys its window there leaves the handler on freed memory. Line numbers refer toc006519.Details
Device::touch_down(Device.cpp:734-745): afteronTouchDown.Invoke, it readswindow->primaryTouchId, writes it andcurrentMousePos,mouseLeftHeldandlastInputTime, makes the windowfocusedWindow, and invokesonMouseLeftClick.~WindowclearsDevice::touchWindowandfocusedWindow(Window.cpp:693-698), not the handler's local.Device::touch_up(Device.cpp:759-765): afteronTouchUp.Invoke, it reads and writeswindow->primaryTouchId,mouseLeftHeldandlastInputTime, and invokesonMouseLeftRelease.Device::touch_motion(:781-786): afteronTouchMove.Invoke, the same withcurrentMousePosandonMouseMove.Device::touch_cancel(:797-802):window->onTouchCancel.Invoke()afteronMouseLeftRelease.Invoke().Window::xdg_surface_handle_configure(Window.cpp:1927-1934):window->Resize(...)reachesonResize.Invoke()(:771,:792); after it the handler runswl_surface_commit(window->surface),window->configured = trueandwindow->ApplyToplevelState().Window::xdg_toplevel_handle_close(:1905-1906):window->open = falseafteronClose.Invoke(). An application that deletes its window fromonCloseis the likeliest of the six.Crafter.Event is built for a listener that destroys the event's owner:
Invokereturns when it findsdeletedset (Crafter.Event.cppm:175and:215at 599144c), and~EventBasesets 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
lastInputTimein the three touch handlers and theApplyToplevelStatecall. 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 afterResizewhen it is not; in the close handler, as Win32 does. Or decide the other way and say so: state inWindow.cppmand inAGENTS.consumer.mdthat a window is not destroyed from its own touch, resize or close listeners.Two more handlers, the keyboard's, with a shape of their own: they keep no local
Window*, but they call throughDevice::keyboardWindowagain after anInvoke, and a listener can have left that static null. Line numbers refer to148ed01.Device::keyboard_keycheckskeyboardWindowonce, on entry (implementations/Crafter.Graphics-Device.cpp:757), invokesonRawKeyHoldoronRawKeyDownthrough it (:761,:764), and then, for a key that brings text, callskeyboardWindow->onTextInput.Invoke(utf8)(:776).Device::TickKeyRepeatschecks it on entry (:819), then callsonRawKeyDown,onRawKeyHoldandonTextInputthrough it for each repeat (:844-847).A listener nulls it by destroying its window (
~Windowclears it,Window.cpp:700), or by dispatching Wayland events when the dispatch brings awl_keyboard.leave(keyboard_leavesets it to null,Device.cpp:738).Clipboard::GetTextdispatches (Clipboard.cpp:341,:368), and so does aWindow'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_leavestops the repeat (:739), andkeyboard_keythen arms it again for that key (:781-786).TickKeyRepeatsdoes nothing whilekeyboardWindowis 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 whilekeyboardWindowis still the window the handler started with, and arm the repeat only then.