From 7e50795ab2aad98a2b502b2175951aa751cd6155 Mon Sep 17 00:00:00 2001 From: tdcosta100 Date: Sun, 3 Sep 2023 18:15:23 -0300 Subject: [PATCH] Windows: protect WGLDisplayConfig from being created multiple times by different threads (#1399) --- platform/windows/src/headless_backend_wgl.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/platform/windows/src/headless_backend_wgl.cpp b/platform/windows/src/headless_backend_wgl.cpp index 84ead0728..e2562c19f 100644 --- a/platform/windows/src/headless_backend_wgl.cpp +++ b/platform/windows/src/headless_backend_wgl.cpp @@ -196,15 +196,10 @@ public: ~WGLDisplayConfig() { UnregisterClassA(renderingWindowClass.lpszClassName, renderingWindowClass.hInstance); } - static std::shared_ptr create() { - static std::weak_ptr instance; - auto shared = instance.lock(); + static WGLDisplayConfig& create() { + static WGLDisplayConfig instance = WGLDisplayConfig{Key{}}; - if (!shared) { - instance = shared = std::make_shared(Key{}); - } - - return shared; + return instance; } public: @@ -219,7 +214,7 @@ public: class WGLBackendImpl final : public HeadlessBackend::Impl { private: - std::shared_ptr wglDisplayConfig = WGLDisplayConfig::create(); + WGLDisplayConfig& wglDisplayConfig = WGLDisplayConfig::create(); HWND renderingWindowHandle = NULL; HDC renderingWindowDeviceContext = NULL; @@ -230,7 +225,7 @@ private: MSG message; renderingWindowHandle = CreateWindowExA(0, - wglDisplayConfig->renderingWindowClass.lpszClassName, + wglDisplayConfig.renderingWindowClass.lpszClassName, "WGL Render Window", WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, @@ -239,7 +234,7 @@ private: 1, NULL, NULL, - wglDisplayConfig->renderingWindowClass.hInstance, + wglDisplayConfig.renderingWindowClass.hInstance, NULL); if (!renderingWindowHandle) { @@ -330,7 +325,7 @@ private: throw std::runtime_error("Failed to set pixel format for context"); } - if (wglDisplayConfig->ARB_create_context) { + if (wglDisplayConfig.ARB_create_context) { renderingWindowRenderingContext = mbgl::platform::wglCreateContextAttribsARB( renderingWindowDeviceContext, NULL,