Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a18838e

Browse files
committed
Add workaround for validation layer memory leak
1 parent 6eb3ff2 commit a18838e

15 files changed

Lines changed: 49 additions & 0 deletions

03_Drawing_a_triangle/03_Drawing/02_Rendering_and_presentation.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,27 @@ You can also wait for operations in a specific command queue to be finished with
348348
perform synchronization. You'll see that the program now exits without problems
349349
when closing the window.
350350

351+
## Memory leak
352+
353+
If you run your application with validation layers enabled and you monitor the
354+
memory usage of your application, you may notice that it is slowly growing. The
355+
reason for this is that the validation layer implementation expects the
356+
application to explicitly synchronize with the GPU. Although this is technically
357+
not required, doing so once a frame will not noticeably affect performance.
358+
359+
We can do this by explicitly waiting for presentation to finish before starting
360+
to draw the next frame:
361+
362+
```c++
363+
void drawFrame() {
364+
...
365+
366+
vkQueuePresentKHR(presentQueue, &presentInfo);
367+
368+
vkQueueWaitIdle(presentQueue);
369+
}
370+
```
371+
351372
## Conclusion
352373

353374
About 800 lines of code later, we've finally gotten to the stage of seeing

03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
204204
} else if (result != VK_SUCCESS) {
205205
throw std::runtime_error("failed to present swap chain image!");
206206
}
207+
208+
vkQueueWaitIdle(presentQueue);
207209
```
208210
209211
The `vkQueuePresentKHR` function returns the same values with the same meaning.

code/depth_buffering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,8 @@ class HelloTriangleApplication {
12831283
} else if (result != VK_SUCCESS) {
12841284
throw std::runtime_error("failed to present swap chain image!");
12851285
}
1286+
1287+
vkQueueWaitIdle(presentQueue);
12861288
}
12871289

12881290
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/descriptor_layout.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,8 @@ class HelloTriangleApplication {
922922
} else if (result != VK_SUCCESS) {
923923
throw std::runtime_error("failed to present swap chain image!");
924924
}
925+
926+
vkQueueWaitIdle(presentQueue);
925927
}
926928

927929
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/descriptor_set.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,8 @@ class HelloTriangleApplication {
976976
} else if (result != VK_SUCCESS) {
977977
throw std::runtime_error("failed to present swap chain image!");
978978
}
979+
980+
vkQueueWaitIdle(presentQueue);
979981
}
980982

981983
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/hello_triangle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ class HelloTriangleApplication {
638638
presentInfo.pImageIndices = &imageIndex;
639639

640640
vkQueuePresentKHR(presentQueue, &presentInfo);
641+
642+
vkQueueWaitIdle(presentQueue);
641643
}
642644

643645
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/index_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,8 @@ class HelloTriangleApplication {
860860
} else if (result != VK_SUCCESS) {
861861
throw std::runtime_error("failed to present swap chain image!");
862862
}
863+
864+
vkQueueWaitIdle(presentQueue);
863865
}
864866

865867
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/model_loading.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,8 @@ class HelloTriangleApplication {
13281328
} else if (result != VK_SUCCESS) {
13291329
throw std::runtime_error("failed to present swap chain image!");
13301330
}
1331+
1332+
vkQueueWaitIdle(presentQueue);
13311333
}
13321334

13331335
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/sampler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,8 @@ class HelloTriangleApplication {
11581158
} else if (result != VK_SUCCESS) {
11591159
throw std::runtime_error("failed to present swap chain image!");
11601160
}
1161+
1162+
vkQueueWaitIdle(presentQueue);
11611163
}
11621164

11631165
VkShaderModule createShaderModule(const std::vector<char>& code) {

code/staging_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,8 @@ class HelloTriangleApplication {
827827
} else if (result != VK_SUCCESS) {
828828
throw std::runtime_error("failed to present swap chain image!");
829829
}
830+
831+
vkQueueWaitIdle(presentQueue);
830832
}
831833

832834
VkShaderModule createShaderModule(const std::vector<char>& code) {

0 commit comments

Comments
 (0)