4 min read

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.)

Destroying a logical device

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.

How to do it…

  1. Take the handle of the created logical device that was stored in a variable of type VkDevice named logical_device.
  2. Call vkDestroyDevice( logical_device, nullptr ), provide the logical_device variable in the first argument and nullptr in the second.
  3. For safety reasons, assign the VK_NULL_HANDLE value the logical_device variable.

How it works…

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.

See also

  • Creating a logical device

Destroying a Vulkan Instance

After all other resources are destroyed, we can destroy a Vulkan Instance.

How to do it…

  1. Take the handle of a created Vulkan Instance object stored in a variable of type VkInstance named instance.
  2. Call vkDestroyInstance( instance, nullptr ), provide an instance variable as the first argument and nullptr as the second argument.
  3. For safety reasons, assign VK_NULL_HANDLE value to the instance variable.

How it works…

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;
}

See also

  • Creating a Vulkan Instance

Releasing a Vulkan Loader library

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.

How to do it…

On Windows operating systems family:

  1. Take the variable of type HMODULE named vulkan_library, in which handle of a loaded Vulkan Loader was stored.
  2. Call FreeLibrary( vulkan_library ), provide a vulkan_library variable in the only argument.
  3. For safeness reasons, assign the nullptr value to the vulkan_library variable.

On Linux operating systems family:

  1. Take the variable of type void* named vulkan_library in which handle to a loaded Vulkan Loader was stored.
  2. Call dlclose( vulkan_library ), provide a vulkan_library variable in the only argument.
  3. For safety reasons, assign the nullptr value to the vulkan_library variable.

How it works…

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;

See also

  • Connecting with a Vulkan Loader library

Summary

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.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here