fix(window): a Window constructor that throws leaves nothing of the window behind
The native constructor registered the window before the steps that can throw: on Wayland it put the window in Device::windows and gave its xdg_surface, its xdg_toplevel and its fractional scale the window as listener data, and on Win32 it made the HWND, whose WM_NCCREATE puts the window in GWLP_USERDATA, and showed it. When a later step threw (a Vulkan error, a surface that offers no storage-writable format), the window had not been constructed, so ~Window did not run: all of that kept pointing at it after the exception, and the Vulkan surface, once made, leaked. #169 has what an application that catches the error and carries on then reads and writes. Window: the constructor delegates to the default constructor, which makes nothing. The body of a delegating constructor runs once its target has returned, and an exception that leaves it invokes the object's destructor ([except.ctor]). So ~Window, the window's one teardown, now also releases what a constructor that threw had made, before the exception reaches the caller. It was already written for members that were never made, which are null or empty and release nothing. That does what the guard the issue suggests would, by the language's own rule: ~Window runs unless construction completes, with nothing of it factored out or repeated in the constructor, and no function-try-block is needed, whose handler may not touch the members. ~Window now runs after a Vulkan command has failed, and the specification leaves such a command's outputs without a defined value ("Error Codes", in its fundamentals: undefined contents, poison ones since 1.4.369), so the Vulkan surface, the swapchain, its image views, the fences and the acquire semaphores are made into a local and go into their members once made. The command buffers still go into theirs directly: an allocation that fails sets every entry to null. The render-finished semaphores went through a local already; their vector now has room for all of them before the first is made, as a push_back that grew it and threw would have left the semaphore just made in no member. CreateSwapchain serves the rebuilds as well, where a vkCreateSwapchainKHR that fails now leaves the member as it was. Tests: WindowConstructorThrow (new, Wayland) makes a window beside another and fails its swapchain's capabilities query, through a function of its own in Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR with Device::surfaceMaintenance1Supported set. It checks that the constructor throws that error; that Device::windows then holds the other window alone; that the failed window's Vulkan surface (the test defines vkDestroySurfaceKHR, which the library calls by name, and passes each call on to the loader) and each of its Wayland objects (read off a WAYLAND_DEBUG log, between the failure and the catch) were destroyed before the exception reached the test; that the compositor raised no protocol error; and that a window made after it renders and closes. The checks of the failed window come before the test dispatches the connection's events again, which without the fix can bring an enter event that walks Device::windows with the freed window in it. WindowConstructorThrowWin32 (new) does the same on Win32 for the HWND and the Vulkan surface, and makes a window after it; it needs Wine, as PointerWin32 does. Under Wine 11.20 it passes. Without the fix both fail: WindowConstructorThrow its checks of Device::windows, of the Vulkan surface and of each Wayland object, and WindowConstructorThrowWin32 those of the HWND and the Vulkan surface. Docs: the README, the constructors' comment in the interface and the consumer notes say that a constructor that throws leaves nothing of the window behind; AGENTS.template.md lists the two tests.
This commit is contained in:
parent
148ed01952
commit
e1ca2fdb54
8 changed files with 489 additions and 10 deletions
|
|
@ -35,6 +35,7 @@ window.StartSync(); // the event loop
|
|||
- A `RenderPass` is `Record(cmd, frameIdx, window)` plus `SwapchainStage()`, which must return the stage that touches the swapchain image (`COMPUTE_SHADER`, `RAY_TRACING_SHADER` or `TRANSFER`): the frame's barriers take their stage and access masks from it. `frameIdx` is the frame slot, `window.currentFrame`, not the image: the frame's image is `window.images[window.currentImage]`. The image stays in `VK_IMAGE_LAYOUT_GENERAL` through the whole pass list, with `STORAGE | TRANSFER_SRC` usage and `TRANSFER_DST` where the surface allows it (`window.swapchainUsage`). Nothing clears it: draw a background quad, or clear from a transfer pass where `TRANSFER_DST` is there.
|
||||
- `window.width` and `height` are physical pixels, as is `currentMousePos`. `CaptureNextFrame(path)` saves the next frame as a PNG. `SetTitle`, `SetAppId`, `SetCursorImage`, `SetDefaultCursor`; `SetTextInput(active, x, y, w, h, purpose, hints)` tells the compositor's input method a text field has focus, which opens an on-screen keyboard on a phone. `purpose` is what the field holds (`TextInputPurpose::Normal`, `Alpha`, `Digits`, `Number`, `Phone`, `Url`, `Email`, `Name`, `Password`, `Pin`, `Date`, `Time`, `Datetime`, `Terminal`), by which the keyboard picks its layout, and `hints` how it treats what is typed (`kTextInputHintCompletion`, `Spellcheck`, `AutoCapitalization`, `Lowercase`, `Uppercase`, `Titlecase`, `HiddenText`, `SensitiveData`, `Latin`, `Multiline`, or-ed together). Without `hints` a field gets `TextInputDefaultHints(purpose)`: completion, spellcheck and capitals for `Normal`, hidden and not learnt for `Password` and `Pin`, none for the rest; `kTextInputHintNone` asks for none. Name the purpose of every field that is not prose. Call it again when the caret moves, with the same purpose and hints. A call that changes either while a field has focus tells the input method the focus moved to another field (it enables anew), which ends the composition under way without an `onPreedit`, as `SetTextInput(false)` does, so commit or drop your composing text first. A move to another field of the same purpose and hints looks to the window like a caret that moved and sends no enable: call `SetTextInput(false)` before the new field's `SetTextInput(true, ...)`. The input method's changes come in batches (text-input-v3's `done`), each fired as `onTextInputDelete`, `onTextInput` and `onPreedit` in that order, and the batches in the order they arrived: one that arrives while a listener of another is running (the listener made a `Window` or called `Clipboard::GetText`, which dispatch the events) waits until that one is through. Only these wait: a key, pointer or touch event the same dispatch brings fires at once, before a batch that waits.
|
||||
- Several windows can exist (on Wayland `Device::windows` lists them; the other backends keep no list), and may share a heap, each writing only its own swapchain slots, but they share one frame clock: one rendering window is the assumption behind the deletion queue, the staging rings and the font atlas's evictions. Without a `Window`, nothing advances `Device::frameCounter`: a program that draws text without one steps it itself after each frame's work, or its full atlas never frees a cell and new glyphs stay blank.
|
||||
- On Wayland and Win32 a `Window` constructor that throws (a Vulkan error, or a surface that offers no format the swapchain can be written through as a storage image) leaves nothing of the window behind (#169): the destructor has run for what it had made by the time the exception reaches you, so on Wayland the window is not in `Device::windows` and its Wayland objects are destroyed, on Win32 its HWND is, and on both so is its Vulkan surface. The error can be caught and the program go on.
|
||||
|
||||
### Device and features
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue