




















































In this article by Pawel Lapinski, the author of the book Vulkan Cookbook, we will learn how to destroy a logical device, destroy a Vulkan Instance and then releasing a Vulkan Loader library.
(For more resources related to this topic, see here.)
After we are done and we want to quit the application, we should cleanup after ourselves. Despite the fact that all the resources should be destroy automatically by the driver, when the Vulkan Instance is destroyed, we should do this explicitly in the application to follow the good programming guidelines. The order of destroying resources should be opposite to the order in which they were created.
Resources should be released in the order reverse to the order of their creation.
In this article logical device was the last created object, so it will be destroyed first.
Implementation of logical device destroying 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, we shouldn't destroy objects that weren't created. Then, we destroy the device with vkDestroyDevice() function call and we assign the VK_NULL_HANDLE value to the variable in which logical device handle was stored. We do this just in case--if there is some kind of 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.
After all other resources are destroyed, we can destroy a Vulkan Instance.
Before we can close the application, we should make sure created resources are released. Vulkan Instance is destroyed with the following code:
if( instance ) {
vkDestroyInstance( instance, nullptr );
instance = VK_NULL_HANDLE;
}
Libraries that are loaded dynamically, must be explicitly closed (released). To be able to use Vulkan in our application, we opened Vulkan Loader (a vulkan-1.dll library on Windows or libvulkan.so.1 library on Linux). So before we can close the application, we should free it.
On Windows operating systems family:
On Linux operating systems family:
On Windows operating systems family, dynamic libraries are opened using LoadLibrary() function. Such libraries must be closed (released) by calling FreeLibrary() function to which a handle of a previously opened library must be provided.
On Linux operating systems family, dynamic libraries are opened using dlopen() function. Such libraries must be closed (released) by calling dlclose() function, to which a handle of a previously opened library must be provided.
#if defined _WIN32
FreeLibrary( vulkan_library );
#elif defined __linux
dlclose( vulkan_library );
#endif
vulkan_library = nullptr;
In this article, you learned about the different members of a class or blueprint. We worked with instance properties, type properties, instance methods, and type methods. We worked with stored properties, getters, setters.
Further resources on this subject: