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
|
|
@ -359,7 +359,21 @@ Window::Window(std::uint32_t width, std::uint32_t height, const std::string_view
|
|||
SetTitle(title);
|
||||
}
|
||||
|
||||
Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height(height) {
|
||||
// Delegates to the default constructor, which makes nothing, so that the
|
||||
// window is a complete object before the first step below. When a step
|
||||
// throws (a Vulkan error, a surface that offers no storage-writable format),
|
||||
// ~Window then runs before the exception leaves the constructor
|
||||
// ([except.ctor]) and releases what had been made by then, as it does for a
|
||||
// window made whole: a member not made by then is null or empty, and
|
||||
// releases nothing. Without that, Device::windows, the HWND's GWLP_USERDATA
|
||||
// and the proxies' listener data went on pointing at the window after the
|
||||
// exception (#169). A Vulkan command that fails leaves its outputs without a
|
||||
// defined value, so each handle goes into its member only once the call that
|
||||
// makes it has succeeded, the command buffers excepted: an allocation that
|
||||
// fails sets them to null.
|
||||
Window::Window(std::uint32_t width, std::uint32_t height) : Window() {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
// Before the platform code below, which may report the pointer.
|
||||
lastMousePos = {0,0};
|
||||
mouseDelta = {0,0};
|
||||
|
|
@ -415,6 +429,7 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
|
|||
wl_surface_commit(surface);
|
||||
#endif
|
||||
|
||||
VkSurfaceKHR madeSurface = VK_NULL_HANDLE;
|
||||
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
||||
// A Unicode class, so the text in WndProc's messages is UTF-16: WM_CHAR
|
||||
// brings UTF-16 code units, and SetTitle's WM_SETTEXT reaches
|
||||
|
|
@ -471,7 +486,7 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
|
|||
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
|
||||
createInfo.hinstance = wc.hInstance;
|
||||
createInfo.hwnd = hwnd;
|
||||
Device::CheckVkResult(vkCreateWin32SurfaceKHR(Device::instance, &createInfo, NULL, &vulkanSurface));
|
||||
Device::CheckVkResult(vkCreateWin32SurfaceKHR(Device::instance, &createInfo, NULL, &madeSurface));
|
||||
#endif
|
||||
|
||||
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||
|
|
@ -479,8 +494,9 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
|
|||
createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
|
||||
createInfo.display = Device::display;
|
||||
createInfo.surface = surface;
|
||||
Device::CheckVkResult(vkCreateWaylandSurfaceKHR(Device::instance, &createInfo, NULL, &vulkanSurface));
|
||||
Device::CheckVkResult(vkCreateWaylandSurfaceKHR(Device::instance, &createInfo, NULL, &madeSurface));
|
||||
#endif
|
||||
vulkanSurface = madeSurface;
|
||||
|
||||
// Get list of supported surface formats
|
||||
std::uint32_t formatCount;
|
||||
|
|
@ -558,6 +574,8 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
|
|||
cmdBufAllocateInfo.commandPool = Device::commandPool;
|
||||
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
cmdBufAllocateInfo.commandBufferCount = numFrames;
|
||||
// Straight into the member: an allocation that fails frees what it had
|
||||
// made and sets every entry to null.
|
||||
Device::CheckVkResult(vkAllocateCommandBuffers(Device::device, &cmdBufAllocateInfo, drawCmdBuffers));
|
||||
|
||||
// Per-frame-in-flight synchronisation objects (issue #40). The fences are
|
||||
|
|
@ -572,8 +590,12 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
|
|||
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
for (std::uint8_t i = 0; i < numFrames; i++) {
|
||||
Device::CheckVkResult(vkCreateFence(Device::device, &fenceCreateInfo, nullptr, &waitFences[i]));
|
||||
Device::CheckVkResult(vkCreateSemaphore(Device::device, &semaphoreCreateInfo, nullptr, &imageAcquiredSemaphores[i]));
|
||||
VkFence fence = VK_NULL_HANDLE;
|
||||
Device::CheckVkResult(vkCreateFence(Device::device, &fenceCreateInfo, nullptr, &fence));
|
||||
waitFences[i] = fence;
|
||||
VkSemaphore semaphore = VK_NULL_HANDLE;
|
||||
Device::CheckVkResult(vkCreateSemaphore(Device::device, &semaphoreCreateInfo, nullptr, &semaphore));
|
||||
imageAcquiredSemaphores[i] = semaphore;
|
||||
}
|
||||
|
||||
// The deferred-deletion queue (issue #101) retires a resource after this
|
||||
|
|
@ -1842,7 +1864,12 @@ void Window::CreateSwapchain()
|
|||
}
|
||||
imageViews.clear();
|
||||
|
||||
Device::CheckVkResult(vkCreateSwapchainKHR(Device::device, &swapchainCI, nullptr, &swapChain));
|
||||
// Each handle reaches its member only once it is made, here and for the
|
||||
// views below: ~Window releases what the members name, also after a
|
||||
// constructor whose CreateSwapchain threw.
|
||||
VkSwapchainKHR madeSwapchain = VK_NULL_HANDLE;
|
||||
Device::CheckVkResult(vkCreateSwapchainKHR(Device::device, &swapchainCI, nullptr, &madeSwapchain));
|
||||
swapChain = madeSwapchain;
|
||||
|
||||
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
|
||||
if (oldSwapchain != VK_NULL_HANDLE) {
|
||||
|
|
@ -1864,7 +1891,11 @@ void Window::CreateSwapchain()
|
|||
|
||||
// A render-finished semaphore per image. The ones the old swapchain's
|
||||
// images used are kept: its last presents may still be waiting on them.
|
||||
// The vector has room for all of them before the first is made: a
|
||||
// push_back that grew it, and threw, would leave the semaphore just made
|
||||
// in no member, where ~Window does not find it.
|
||||
const VkSemaphoreCreateInfo semaphoreCreateInfo { .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
||||
renderFinishedSemaphores.reserve(imageCount);
|
||||
while (renderFinishedSemaphores.size() < imageCount) {
|
||||
VkSemaphore semaphore = VK_NULL_HANDLE;
|
||||
Device::CheckVkResult(vkCreateSemaphore(Device::device, &semaphoreCreateInfo, nullptr, &semaphore));
|
||||
|
|
@ -1901,7 +1932,9 @@ void Window::CreateSwapchain()
|
|||
// once per frame whose image changed.
|
||||
imageViews.resize(imageCount);
|
||||
for (std::uint32_t i = 0; i < imageCount; i++) {
|
||||
Device::CheckVkResult(vkCreateImageView(Device::device, &imageViewInfos[i], nullptr, &imageViews[i]));
|
||||
VkImageView view = VK_NULL_HANDLE;
|
||||
Device::CheckVkResult(vkCreateImageView(Device::device, &imageViewInfos[i], nullptr, &view));
|
||||
imageViews[i] = view;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue