vblanco20-1 / vulkan-guide

Introductory guide to vulkan.
https://vkguide.dev/
MIT License
940 stars 213 forks source link

New Chapter 2, Push Constants and new shaders -- 0xC0000005: Access violation reading location #95

Closed tonebacas closed 4 months ago

tonebacas commented 9 months ago

I was following along the 'new' guide for Vulkan 1.3 with dynamic rendering, and got a runtime error when exiting application, during cleanup() function ( _mainDeletionQueue.flush(); ).

The lambda expression is capturing by reference, but in its body, the variables sky and gradient reference garbage data, as they are not fields of the VulkanEngine class.

void VulkanEngine::init_background_pipelines() {
  // ...
  _mainDeletionQueue.push_function([&]() {
      vkDestroyPipelineLayout(_device, _gradientPipelineLayout, nullptr);
      vkDestroyPipeline(_device, sky.pipeline, nullptr);
      vkDestroyPipeline(_device, gradient.pipeline, nullptr);
  });
}

https://github.com/vblanco20-1/vulkan-guide/blame/4fafdfee151fea55d036c727b3b0b372d1c9239e/docs/new_chapter_2/vulkan_pushconstants.md#L215C2-L215C2

I got it working by replacing the lines responsible for pipeline destruction that were using sky and gradient by reference with a for loop, and also destroying _gradientPipelineLayout last, like this:

  _mainDeletionQueue.push_function([&]() {
    for (auto it = backgroundEffects.rbegin(); it != backgroundEffects.rend(); it++){
        vkDestroyPipeline(_device, it->pipeline, nullptr);
    }
    vkDestroyPipelineLayout(_device, _gradientPipelineLayout, nullptr);
    });
vblanco20-1 commented 4 months ago

This has been fixed now