Fix crash when exiting Dawn samples
This patch fixes a crash (nullptr access) when closing the window of
Dawn samples by manually destroying all WGPU objects in main()
function. Without this step `wireHelper` (in SampleUtils.cpp) will
always be destroyed before any WGPU objects in each sample file, so a
nullptr access will occur when any WGPU object is destroyed as at that
time `dawnProcSetProcs(nullptr)` has already been called in
`~WireHelper()`.
Bug: None
Change-Id: If531b76a31352e315b39963265ebcf7c7b9b6064
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/183480
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/dawn/samples/Animometer.cpp b/src/dawn/samples/Animometer.cpp
index 6a992e9..95cae31 100644
--- a/src/dawn/samples/Animometer.cpp
+++ b/src/dawn/samples/Animometer.cpp
@@ -43,6 +43,15 @@
wgpu::BindGroup bindGroup;
wgpu::Buffer ubo;
+void destroyAllObjects() {
+ queue = nullptr;
+ swapchain = nullptr;
+ pipeline = nullptr;
+ bindGroup = nullptr;
+ ubo = nullptr;
+ device = nullptr;
+}
+
float RandomFloat(float min, float max) {
// NOLINTNEXTLINE(runtime/threadsafe_fn)
float zeroOne = rand() / static_cast<float>(RAND_MAX);
@@ -205,4 +214,6 @@
timer->Start();
}
}
+
+ destroyAllObjects();
}
diff --git a/src/dawn/samples/ComputeBoids.cpp b/src/dawn/samples/ComputeBoids.cpp
index 6062aff..a32f71d 100644
--- a/src/dawn/samples/ComputeBoids.cpp
+++ b/src/dawn/samples/ComputeBoids.cpp
@@ -70,6 +70,19 @@
int particleCount;
};
+void destroyAllObjects() {
+ device = nullptr;
+ queue = nullptr;
+ swapchain = nullptr;
+ depthStencilView = nullptr;
+ modelBuffer = nullptr;
+ particleBuffers.fill(nullptr);
+ renderPipeline = nullptr;
+ updateParams = nullptr;
+ updatePipeline = nullptr;
+ updateBGs.fill(nullptr);
+}
+
void initBuffers() {
std::array<std::array<float, 2>, 3> model = {{
{-0.01, -0.02},
@@ -338,4 +351,6 @@
frame();
dawn::utils::USleep(16000);
}
+
+ destroyAllObjects();
}
diff --git a/src/dawn/samples/CppHelloTriangle.cpp b/src/dawn/samples/CppHelloTriangle.cpp
index b1b47a5..c1df97b 100644
--- a/src/dawn/samples/CppHelloTriangle.cpp
+++ b/src/dawn/samples/CppHelloTriangle.cpp
@@ -47,6 +47,19 @@
wgpu::RenderPipeline pipeline;
wgpu::BindGroup bindGroup;
+void destroyAllObjects() {
+ device = nullptr;
+ indexBuffer = nullptr;
+ vertexBuffer = nullptr;
+ texture = nullptr;
+ sampler = nullptr;
+ queue = nullptr;
+ swapchain = nullptr;
+ depthStencilView = nullptr;
+ pipeline = nullptr;
+ bindGroup = nullptr;
+}
+
void initBuffers() {
static const uint32_t indexData[3] = {
0,
@@ -192,4 +205,6 @@
frame();
dawn::utils::USleep(16000);
}
+
+ destroyAllObjects();
}