Destroying a logical device
After we have finished and we want to quit the application, we should clean up after ourselves. Despite the fact that all the resources should be destroyed automatically by the driver when the Vulkan Instance is destroyed, we should also do this explicitly in the application to follow good programming guidelines. The order of destroying resources should be opposite to the order in which they were created.
Note
Resources should be released in the reverse order to the order of their creation.
In this chapter, the logical device was the last created object, so it will be destroyed first.
How to do it...
- Take the handle of the logical device that was created and stored in a variable of type
VkDevice
namedlogical_device
. - Call
vkDestroyDevice( logical_device, nullptr )
; provide thelogical_device
variable in the first argument, and anullptr
value in the second. - For safety reasons, assign the
VK_NULL_HANDLE
value to thelogical_device
variable.
How it works...
The implementation of the logical device-destroying recipe is very straightforward:
if( logical_device ) { vkDestroyDevice( logical_device, nullptr ); logical_device = VK_NULL_HANDLE; }
First, we need to check if the logical device handle is valid, because, we shouldn't destroy objects that weren't created. Then, we destroy the device with the vkDestroyDevice()
function call and we assign the VK_NULL_HANDLE
value to the variable in which the logical device handle was stored. We do this just in case--if there is a mistake in our code, we won't destroy the same object twice.
Remember that, when we destroy a logical device, we can't use device-level functions acquired from it.
See also
- The recipe Creating a logical device in this chapter