Use smart pointer for the LibX11Exports singleton
Currently we use a raw pointer to store the LibX11Exports
for X11 WSI. This triggers LeakSanitizer errors since the
memory allocated from new() is never deleted. We can fix
this by using a unique_ptr instead.
TEST=escher_unittests on linux hosts w/ LSan enabled.
Bug: fuchsia:78410
Change-Id: Ifbe284ce807df0d3da5811bc327a6dc4f2b3a3e7
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/58189
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Commit-Queue: Yilong Li <liyl@google.com>
Tested-by: Yilong Li <liyl@google.com>
diff --git a/src/WSI/libX11.cpp b/src/WSI/libX11.cpp
index 977dc4e..72d9fdb 100644
--- a/src/WSI/libX11.cpp
+++ b/src/WSI/libX11.cpp
@@ -15,6 +15,7 @@
#include "libX11.hpp"
#include "System/SharedLibrary.hpp"
+#include <memory>
namespace {
@@ -61,13 +62,13 @@
{
static void *libX11 = nullptr;
static void *libXext = nullptr;
- static LibX11exports *libX11exports = nullptr;
+ static std::unique_ptr<LibX11exports> libX11exports = nullptr;
if(!libX11)
{
if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library.
{
- libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
+ libX11exports = std::make_unique<LibX11exports>(RTLD_DEFAULT, RTLD_DEFAULT);
libX11 = (void *)-1; // No need to load it.
}
else
@@ -77,7 +78,7 @@
if(libX11)
{
libXext = loadLibrary("libXext.so");
- libX11exports = new LibX11exports(libX11, libXext);
+ libX11exports = std::make_unique<LibX11exports>(libX11, libXext);
}
else
{
@@ -86,7 +87,7 @@
}
}
- return libX11exports;
+ return libX11exports.get();
}
LibX11 libX11;