140 lines
6 KiB
C++
140 lines
6 KiB
C++
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// Issue #169, on Win32: a Window whose constructor throws leaves nothing
|
||
|
|
// behind. The constructor makes the HWND, whose WM_NCCREATE puts the window
|
||
|
|
// in GWLP_USERDATA, and shows it, before the steps that can throw: the
|
||
|
|
// Vulkan surface, the surface's formats and capabilities, the swapchain.
|
||
|
|
// ~Window, which detaches the HWND and destroys it and the Vulkan surface,
|
||
|
|
// did not run for a window whose constructor threw, and the exception
|
||
|
|
// released the window's storage: the HWND stayed on screen with
|
||
|
|
// GWLP_USERDATA pointing at the freed window, which WndProc reads and writes
|
||
|
|
// for the messages it handles, and the Vulkan surface leaked.
|
||
|
|
//
|
||
|
|
// As WindowConstructorThrow does on Wayland, the test fails the swapchain's
|
||
|
|
// capabilities query of the constructor: with
|
||
|
|
// Device::surfaceMaintenance1Supported set, the query goes through
|
||
|
|
// Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR, where the test puts a
|
||
|
|
// function of its own that fails with VK_ERROR_SURFACE_LOST_KHR. Another
|
||
|
|
// window stays open throughout. Checked:
|
||
|
|
//
|
||
|
|
// - the constructor throws the error Device::CheckVkResult makes of that
|
||
|
|
// result
|
||
|
|
// - the HWND it had made was destroyed
|
||
|
|
// - its Vulkan surface was destroyed before the exception reached the
|
||
|
|
// caller (the test defines vkDestroySurfaceKHR, which the library calls
|
||
|
|
// by name, and passes each call on to vulkan-1.dll's)
|
||
|
|
// - a window can be made after it
|
||
|
|
//
|
||
|
|
// Built on Linux, it runs under Wine:
|
||
|
|
// crafter-build test --target=x86_64-w64-mingw32 WindowConstructorThrowWin32
|
||
|
|
|
||
|
|
#include <windows.h>
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstdlib>
|
||
|
|
#include "vulkan/vulkan.h"
|
||
|
|
|
||
|
|
import Crafter.Graphics;
|
||
|
|
import std;
|
||
|
|
using namespace Crafter;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
int failures = 0;
|
||
|
|
|
||
|
|
// Each line flushed as it is printed: without the fix the window that failed
|
||
|
|
// is still there for the messages pumped after the checks, which can crash
|
||
|
|
// the test, and a buffered stdout would lose the lines before.
|
||
|
|
void Check(bool ok, std::string_view what) {
|
||
|
|
std::println("{} {}", ok ? "PASS" : "FAIL", what);
|
||
|
|
std::fflush(stdout);
|
||
|
|
if (!ok) ++failures;
|
||
|
|
}
|
||
|
|
|
||
|
|
// The other window's HWND, set before the query is made to fail.
|
||
|
|
HWND otherHwnd = nullptr;
|
||
|
|
|
||
|
|
// What the failing capabilities query saw of the window being made.
|
||
|
|
struct Failed {
|
||
|
|
int calls = 0;
|
||
|
|
HWND hwnd = nullptr;
|
||
|
|
LONG_PTR userData = 0;
|
||
|
|
VkSurfaceKHR surface = VK_NULL_HANDLE;
|
||
|
|
};
|
||
|
|
Failed failed;
|
||
|
|
|
||
|
|
// Every surface the library passed to vkDestroySurfaceKHR, in order.
|
||
|
|
std::vector<VkSurfaceKHR> destroyedSurfaces;
|
||
|
|
|
||
|
|
// The window being made: of the other window's class, on this thread, and
|
||
|
|
// not the other window.
|
||
|
|
BOOL CALLBACK FindMade(HWND hwnd, LPARAM) {
|
||
|
|
if (hwnd != otherHwnd && GetClassLongPtrW(hwnd, GCW_ATOM) == GetClassLongPtrW(otherHwnd, GCW_ATOM)) {
|
||
|
|
failed.hwnd = hwnd;
|
||
|
|
failed.userData = GetWindowLongPtrW(hwnd, GWLP_USERDATA);
|
||
|
|
}
|
||
|
|
return TRUE;
|
||
|
|
}
|
||
|
|
|
||
|
|
VkResult VKAPI_CALL FailCapabilities(VkPhysicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* info, VkSurfaceCapabilities2KHR*) {
|
||
|
|
++failed.calls;
|
||
|
|
failed.surface = info->surface;
|
||
|
|
EnumThreadWindows(GetCurrentThreadId(), FindMade, 0);
|
||
|
|
return VK_ERROR_SURFACE_LOST_KHR;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
// The library calls vkDestroySurfaceKHR by name, so it reaches this
|
||
|
|
// definition, which notes the surface and passes the call on to
|
||
|
|
// vulkan-1.dll's.
|
||
|
|
extern "C" VKAPI_ATTR void VKAPI_CALL vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks* allocator) {
|
||
|
|
destroyedSurfaces.push_back(surface);
|
||
|
|
static const auto next = reinterpret_cast<PFN_vkDestroySurfaceKHR>(
|
||
|
|
reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"vulkan-1.dll"), "vkDestroySurfaceKHR")));
|
||
|
|
if (next != nullptr) next(instance, surface, allocator);
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
Device::Initialize();
|
||
|
|
auto other = std::make_unique<Window>(160, 120, "WindowConstructorThrowWin32: the other window");
|
||
|
|
otherHwnd = other->hwnd;
|
||
|
|
|
||
|
|
const bool surfaceMaintenance1 = Device::surfaceMaintenance1Supported;
|
||
|
|
const PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR capabilities2 = Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR;
|
||
|
|
Device::surfaceMaintenance1Supported = true;
|
||
|
|
Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR = FailCapabilities;
|
||
|
|
std::optional<std::string> thrown;
|
||
|
|
std::size_t destroyedAtCatch = 0;
|
||
|
|
try {
|
||
|
|
auto window = std::make_unique<Window>(320, 240, "WindowConstructorThrowWin32: the window that fails");
|
||
|
|
} catch (const std::runtime_error& error) {
|
||
|
|
thrown = error.what();
|
||
|
|
destroyedAtCatch = destroyedSurfaces.size();
|
||
|
|
}
|
||
|
|
Device::surfaceMaintenance1Supported = surfaceMaintenance1;
|
||
|
|
Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR = capabilities2;
|
||
|
|
|
||
|
|
Check(thrown == "VK_ERROR_SURFACE_LOST_KHR",
|
||
|
|
std::format("the constructor throws the error of the failed query (threw: {})", thrown.value_or("nothing")));
|
||
|
|
Check(failed.calls == 1 && failed.hwnd != nullptr && failed.userData != 0,
|
||
|
|
std::format("the query failed once, inside the constructor of a window whose HWND names it ({} calls)", failed.calls));
|
||
|
|
Check(failed.hwnd != nullptr && !IsWindow(failed.hwnd), "the failed window's HWND was destroyed");
|
||
|
|
Check(failed.surface != VK_NULL_HANDLE
|
||
|
|
&& std::ranges::contains(std::span(destroyedSurfaces).first(destroyedAtCatch), failed.surface),
|
||
|
|
"the failed window's Vulkan surface was destroyed before the exception reached the caller");
|
||
|
|
|
||
|
|
auto after = std::make_unique<Window>(320, 240, "WindowConstructorThrowWin32: the window after");
|
||
|
|
const HWND afterHwnd = after->hwnd;
|
||
|
|
Check(IsWindow(afterHwnd) && IsWindow(otherHwnd), "a window can be made after it, beside the other window");
|
||
|
|
after.reset();
|
||
|
|
other.reset();
|
||
|
|
Check(!IsWindow(afterHwnd) && !IsWindow(otherHwnd), "and both close");
|
||
|
|
|
||
|
|
if (failures != 0) {
|
||
|
|
std::println("{} check(s) failed", failures);
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
std::println("all checks passed");
|
||
|
|
return EXIT_SUCCESS;
|
||
|
|
}
|