Skip to content

Commit

Permalink
Use custom std::unique_ptr deleter to more safely handle owning poi…
Browse files Browse the repository at this point in the history
…nter
  • Loading branch information
ChrisThrasher committed Oct 15, 2024
1 parent 93c48c1 commit 3d1c78d
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/SFML/Window/Unix/VulkanImplX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,32 @@ namespace
{
struct VulkanLibraryWrapper
{
~VulkanLibraryWrapper()
{
if (library)
dlclose(library);
}

// Try to load the library and all the required entry points
bool loadLibrary()
{
if (library)
return true;

library = dlopen("libvulkan.so.1", RTLD_LAZY);
library.reset(dlopen("libvulkan.so.1", RTLD_LAZY));

if (!library)
return false;

if (!loadEntryPoint(vkGetInstanceProcAddr, "vkGetInstanceProcAddr"))
{
dlclose(library);
library = nullptr;
library.reset();
return false;
}

if (!loadEntryPoint(vkEnumerateInstanceLayerProperties, "vkEnumerateInstanceLayerProperties"))
{
dlclose(library);
library = nullptr;
library.reset();
return false;
}

if (!loadEntryPoint(vkEnumerateInstanceExtensionProperties, "vkEnumerateInstanceExtensionProperties"))
{
dlclose(library);
library = nullptr;
library.reset();
return false;
}

Expand All @@ -87,12 +78,19 @@ struct VulkanLibraryWrapper
template <typename T>
bool loadEntryPoint(T& entryPoint, const char* name)
{
entryPoint = reinterpret_cast<T>(dlsym(library, name));
entryPoint = reinterpret_cast<T>(dlsym(library.get(), name));

return entryPoint != nullptr;
}

void* library{};
struct SharedLibraryDeleter
{
void operator()(void* ptr) const
{
dlclose(ptr);
}
};
std::unique_ptr<void, SharedLibraryDeleter> library;

PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr{};
PFN_vkEnumerateInstanceLayerProperties vkEnumerateInstanceLayerProperties{};
Expand Down Expand Up @@ -169,7 +167,7 @@ VulkanFunctionPointer VulkanImpl::getFunction(const char* name)
if (!isAvailable(false))
return nullptr;

return reinterpret_cast<VulkanFunctionPointer>(dlsym(wrapper.library, name));
return reinterpret_cast<VulkanFunctionPointer>(dlsym(wrapper.library.get(), name));
}


Expand Down

0 comments on commit 3d1c78d

Please sign in to comment.