Currently using Ubuntu 20.04 LTS, g++11.1.0, C++20, SDL2
Compiler Flags: -lvulkan -ldl -lSDL2main -lSDL2
Code:
#include "VkBootstrap.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
#include <vulkan/vulkan.hpp>
#include <iostream>
int main()
{
VkExtent2D windowExtent {1600, 900};
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO))
{
std::cerr << "Unable to initialize SDL: " << SDL_GetError();
std::abort();
}
// Create a SDL Window
SDL_Window* window {SDL_CreateWindow(
"Vulkan",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
windowExtent.width,
windowExtent.height,
SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE
)};
// Check if window was created sucessfully
if (!window)
{
std::cerr << "Failed to create SDL Window: " << SDL_GetError();
std::abort();
}
vkb::InstanceBuilder instanceBuilder {};
// Initialize the Vulkan instance, with basic debug features
auto builderResult {instanceBuilder
.set_app_name("Vulkan Game")
.request_validation_layers(true)
.use_default_debug_messenger()
.require_api_version(1, 2, 0)
.build()};
vkb::Instance vkbInstance {builderResult.value()};
// Store the Instance
VkInstance instance {vkbInstance.instance};
// Store the Debug Messenger
VkDebugUtilsMessengerEXT debugMessenger {vkbInstance.debug_messenger};
VkSurfaceKHR surface {};
// Get a Vulkan Rendering Surface of the SDL Window
if (!SDL_Vulkan_CreateSurface(window, instance, &surface))
{
std::cerr << "Unable to Create Vulkan Rendering Surface.\n";
std::abort();
}
vkb::PhysicalDeviceSelector gpuSelector {vkbInstance};
// Initialize the Physical Device with a GPU that can render to the window
vkb::PhysicalDevice vkbPhysicalDevice {gpuSelector
.set_minimum_version(1, 2)
.set_surface(surface)
.select()
.value()};
// Store the Vulkan Physical Device
VkPhysicalDevice physicalDevice {vkbPhysicalDevice.physical_device};
// Surface Deletion
vkDestroySurfaceKHR(instance, surface, nullptr);
// Debug Messenger Deletion
vkb::destroy_debug_utils_messenger(instance, debugMessenger);
// Instance Deletion
vkDestroyInstance(instance, nullptr);
// Window Deletion
SDL_DestroyWindow(window);
}
Following the tutorial https://vkguide.dev/ and using the VkBootstrap library, when I try to select the physical device, it causes a memory leak like the one shown below:
=================================================================
==44934==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 576 byte(s) in 4 object(s) allocated from:
#0 0x7f8e9c905e17 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
#1 0x7f8e97a13a1d (/lib/x86_64-linux-gnu/libdrm.so.2+0x4a1d)
#2 0x31647261632e (<unknown module>)
Direct leak of 128 byte(s) in 4 object(s) allocated from:
#0 0x7f8e9c905c47 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x7f8e9b35a0c5 (/lib/x86_64-linux-gnu/libxcb.so.1+0xe0c5)
SUMMARY: AddressSanitizer: 704 byte(s) leaked in 8 allocation(s).
I ran a diagnostic on the available GPUs by modifying the VkBootstrap.cpp file to output data, and got this:
GPU: Intel(R) HD Graphics 530 (SKL GT2)
Api Version: 4202641
Device ID: 6418
Device Type: 1
Driver Version: 88080387
GPU: llvmpipe (LLVM 12.0.0, 256 bits)
Api Version: 4194306
Device ID: 0
Device Type: 4
Driver Version: 1
Selected GPU: Intel(R) HD Graphics 530 (SKL GT2)
Although I have an Nvidia and Intel graphics card, how come it isn't showing the Nvidia graphics card as a viable GPU?
Also, why are these memory leaks occurring? How can I resolve them?
