Skip to content

Commit f87c2d3

Browse files
committed
Add exception handling for vk::SystemError across mainLoop and presentation functions
- Catch `vk::SystemError` in `mainLoop` and presentation logic to handle `vk::Result::eErrorOutOfDateKHR` gracefully. - Log and ignore swapchain-related errors during shutdown or presentation to prevent crashes while rethrowing unexpected exceptions. - Updated multiple attachments for consistent error handling.
1 parent 07d4d97 commit f87c2d3

17 files changed

+335
-82
lines changed

attachments/15_hello_triangle.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,18 @@ class HelloTriangleApplication {
100100
void mainLoop() {
101101
while (!glfwWindowShouldClose(window)) {
102102
glfwPollEvents();
103-
drawFrame();
103+
try {
104+
drawFrame();
105+
} catch (const vk::SystemError& e) {
106+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
107+
// Swapchain is out of date, this can happen during window close
108+
// Just ignore and continue to close
109+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
110+
} else {
111+
// Rethrow other errors
112+
throw;
113+
}
114+
}
104115
}
105116

106117
device.waitIdle();

attachments/16_frames_in_flight.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,18 @@ class HelloTriangleApplication {
104104
void mainLoop() {
105105
while (!glfwWindowShouldClose(window)) {
106106
glfwPollEvents();
107-
drawFrame();
107+
try {
108+
drawFrame();
109+
} catch (const vk::SystemError& e) {
110+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
111+
// Swapchain is out of date, this can happen during window close
112+
// Just ignore and continue to close
113+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
114+
} else {
115+
// Rethrow other errors
116+
throw;
117+
}
118+
}
108119
}
109120

110121
device.waitIdle();

attachments/17_swap_chain_recreation.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,18 @@ class HelloTriangleApplication {
111111
void mainLoop() {
112112
while (!glfwWindowShouldClose(window)) {
113113
glfwPollEvents();
114-
drawFrame();
114+
try {
115+
drawFrame();
116+
} catch (const vk::SystemError& e) {
117+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
118+
// Swapchain is out of date, this can happen during window close
119+
// Just ignore and continue to close
120+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
121+
} else {
122+
// Rethrow other errors
123+
throw;
124+
}
125+
}
115126
}
116127

117128
device.waitIdle();
@@ -535,14 +546,25 @@ class HelloTriangleApplication {
535546
graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]);
536547

537548

538-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
539-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
540-
result = presentQueue.presentKHR( presentInfoKHR );
541-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
542-
framebufferResized = false;
543-
recreateSwapChain();
544-
} else if (result != vk::Result::eSuccess) {
545-
throw std::runtime_error("failed to present swap chain image!");
549+
try {
550+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
551+
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
552+
result = presentQueue.presentKHR( presentInfoKHR );
553+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
554+
framebufferResized = false;
555+
recreateSwapChain();
556+
} else if (result != vk::Result::eSuccess) {
557+
throw std::runtime_error("failed to present swap chain image!");
558+
}
559+
} catch (const vk::SystemError& e) {
560+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
561+
// Swapchain is out of date, this can happen during window close
562+
// Just ignore and continue to close
563+
std::cout << "Ignoring ErrorOutOfDateKHR during presentation" << std::endl;
564+
} else {
565+
// Rethrow other errors
566+
throw;
567+
}
546568
}
547569
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
548570
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

attachments/18_vertex_input.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,18 @@ class HelloTriangleApplication {
135135
void mainLoop() {
136136
while (!glfwWindowShouldClose(window)) {
137137
glfwPollEvents();
138-
drawFrame();
138+
try {
139+
drawFrame();
140+
} catch (const vk::SystemError& e) {
141+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
142+
// Swapchain is out of date, this can happen during window close
143+
// Just ignore and continue to close
144+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
145+
} else {
146+
// Rethrow other errors
147+
throw;
148+
}
149+
}
139150
}
140151

141152
device.waitIdle();
@@ -562,14 +573,25 @@ class HelloTriangleApplication {
562573
graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]);
563574

564575

565-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
566-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
567-
result = presentQueue.presentKHR( presentInfoKHR );
568-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
569-
framebufferResized = false;
570-
recreateSwapChain();
571-
} else if (result != vk::Result::eSuccess) {
572-
throw std::runtime_error("failed to present swap chain image!");
576+
try {
577+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
578+
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
579+
result = presentQueue.presentKHR( presentInfoKHR );
580+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
581+
framebufferResized = false;
582+
recreateSwapChain();
583+
} else if (result != vk::Result::eSuccess) {
584+
throw std::runtime_error("failed to present swap chain image!");
585+
}
586+
} catch (const vk::SystemError& e) {
587+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
588+
// Swapchain is out of date, this can happen during window close
589+
// Just ignore and continue to close
590+
std::cout << "Ignoring ErrorOutOfDateKHR during presentation" << std::endl;
591+
} else {
592+
// Rethrow other errors
593+
throw;
594+
}
573595
}
574596
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
575597
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

attachments/19_vertex_buffer.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,18 @@ class HelloTriangleApplication {
139139
void mainLoop() {
140140
while (!glfwWindowShouldClose(window)) {
141141
glfwPollEvents();
142-
drawFrame();
142+
try {
143+
drawFrame();
144+
} catch (const vk::SystemError& e) {
145+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
146+
// Swapchain is out of date, this can happen during window close
147+
// Just ignore and continue to close
148+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
149+
} else {
150+
// Rethrow other errors
151+
throw;
152+
}
153+
}
143154
}
144155

145156
device.waitIdle();
@@ -592,14 +603,25 @@ class HelloTriangleApplication {
592603
graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]);
593604

594605

595-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
596-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
597-
result = presentQueue.presentKHR( presentInfoKHR );
598-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
599-
framebufferResized = false;
600-
recreateSwapChain();
601-
} else if (result != vk::Result::eSuccess) {
602-
throw std::runtime_error("failed to present swap chain image!");
606+
try {
607+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
608+
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
609+
result = presentQueue.presentKHR( presentInfoKHR );
610+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
611+
framebufferResized = false;
612+
recreateSwapChain();
613+
} else if (result != vk::Result::eSuccess) {
614+
throw std::runtime_error("failed to present swap chain image!");
615+
}
616+
} catch (const vk::SystemError& e) {
617+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
618+
// Swapchain is out of date, this can happen during window close
619+
// Just ignore and continue to close
620+
std::cout << "Ignoring ErrorOutOfDateKHR during presentation" << std::endl;
621+
} else {
622+
// Rethrow other errors
623+
throw;
624+
}
603625
}
604626
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
605627
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

attachments/20_staging_buffer.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,18 @@ class HelloTriangleApplication {
139139
void mainLoop() {
140140
while (!glfwWindowShouldClose(window)) {
141141
glfwPollEvents();
142-
drawFrame();
142+
try {
143+
drawFrame();
144+
} catch (const vk::SystemError& e) {
145+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
146+
// Swapchain is out of date, this can happen during window close
147+
// Just ignore and continue to close
148+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
149+
} else {
150+
// Rethrow other errors
151+
throw;
152+
}
153+
}
143154
}
144155

145156
device.waitIdle();
@@ -611,14 +622,25 @@ class HelloTriangleApplication {
611622
graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]);
612623

613624

614-
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
615-
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
616-
result = presentQueue.presentKHR( presentInfoKHR );
617-
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
618-
framebufferResized = false;
619-
recreateSwapChain();
620-
} else if (result != vk::Result::eSuccess) {
621-
throw std::runtime_error("failed to present swap chain image!");
625+
try {
626+
const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex],
627+
.swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex };
628+
result = presentQueue.presentKHR( presentInfoKHR );
629+
if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) {
630+
framebufferResized = false;
631+
recreateSwapChain();
632+
} else if (result != vk::Result::eSuccess) {
633+
throw std::runtime_error("failed to present swap chain image!");
634+
}
635+
} catch (const vk::SystemError& e) {
636+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
637+
// Swapchain is out of date, this can happen during window close
638+
// Just ignore and continue to close
639+
std::cout << "Ignoring ErrorOutOfDateKHR during presentation" << std::endl;
640+
} else {
641+
// Rethrow other errors
642+
throw;
643+
}
622644
}
623645
semaphoreIndex = (semaphoreIndex + 1) % presentCompleteSemaphore.size();
624646
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

attachments/21_index_buffer.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,18 @@ class HelloTriangleApplication {
147147
void mainLoop() {
148148
while (!glfwWindowShouldClose(window)) {
149149
glfwPollEvents();
150-
drawFrame();
150+
try {
151+
drawFrame();
152+
} catch (const vk::SystemError& e) {
153+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
154+
// Swapchain is out of date, this can happen during window close
155+
// Just ignore and continue to close
156+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
157+
} else {
158+
// Rethrow other errors
159+
throw;
160+
}
161+
}
151162
}
152163

153164
device.waitIdle();

attachments/22_descriptor_layout.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,18 @@ class HelloTriangleApplication {
164164
void mainLoop() {
165165
while (!glfwWindowShouldClose(window)) {
166166
glfwPollEvents();
167-
drawFrame();
167+
try {
168+
drawFrame();
169+
} catch (const vk::SystemError& e) {
170+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
171+
// Swapchain is out of date, this can happen during window close
172+
// Just ignore and continue to close
173+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
174+
} else {
175+
// Rethrow other errors
176+
throw;
177+
}
178+
}
168179
}
169180

170181
device.waitIdle();

attachments/23_descriptor_sets.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,18 @@ class HelloTriangleApplication {
169169
void mainLoop() {
170170
while (!glfwWindowShouldClose(window)) {
171171
glfwPollEvents();
172-
drawFrame();
172+
try {
173+
drawFrame();
174+
} catch (const vk::SystemError& e) {
175+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
176+
// Swapchain is out of date, this can happen during window close
177+
// Just ignore and continue to close
178+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
179+
} else {
180+
// Rethrow other errors
181+
throw;
182+
}
183+
}
173184
}
174185

175186
device.waitIdle();

attachments/24_texture_image.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,18 @@ class HelloTriangleApplication {
176176
void mainLoop() {
177177
while (!glfwWindowShouldClose(window)) {
178178
glfwPollEvents();
179-
drawFrame();
179+
try {
180+
drawFrame();
181+
} catch (const vk::SystemError& e) {
182+
if (e.code().value() == static_cast<int>(vk::Result::eErrorOutOfDateKHR)) {
183+
// Swapchain is out of date, this can happen during window close
184+
// Just ignore and continue to close
185+
std::cout << "Ignoring ErrorOutOfDateKHR during shutdown" << std::endl;
186+
} else {
187+
// Rethrow other errors
188+
throw;
189+
}
190+
}
180191
}
181192

182193
device.waitIdle();

0 commit comments

Comments
 (0)