diff --git a/CHANGELOG.md b/CHANGELOG.md index 788be084012a..b26d5e532287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ ### 🐞 Bug fixes - *...Add new stuff here...* +- [core] Fix thread-unsafe headless OpenGL display singleton initialization [#4332](https://github.com/maplibre/maplibre-native/pull/4332) - [macos] Fix `mlt-cpp` and `mbgl-vendor-icu` not being included in the amalgamation - [core] Fix memory access violation exception in vector_tile_data.cpp [#632](https://github.com/maplibre/maplibre-native/pull/632) - [iOS] Fix a bug where the compass was determined to be misplaced when hidden [#498](https://github.com/maplibre/maplibre-native/pull/498). diff --git a/platform/linux/src/headless_backend_egl.cpp b/platform/linux/src/headless_backend_egl.cpp index 5d412b08bcfa..02e517eede1f 100644 --- a/platform/linux/src/headless_backend_egl.cpp +++ b/platform/linux/src/headless_backend_egl.cpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace mbgl { @@ -51,12 +52,9 @@ class EGLDisplayConfig { ~EGLDisplayConfig() { eglTerminate(display); } static std::shared_ptr create() { - static std::weak_ptr instance; - auto shared = instance.lock(); - if (!shared) { - instance = shared = std::make_shared(Key{}); - } - return shared; + // C++11 magic static guarantees thread-safe one-shot initialization. + static const auto instance = std::make_shared(Key{}); + return instance; } public: diff --git a/platform/linux/src/headless_backend_glx.cpp b/platform/linux/src/headless_backend_glx.cpp index bf13ac81f2a5..2da16e99dc71 100644 --- a/platform/linux/src/headless_backend_glx.cpp +++ b/platform/linux/src/headless_backend_glx.cpp @@ -3,6 +3,7 @@ #include #include +#include #ifdef CI_BUILD #include @@ -80,12 +81,9 @@ class GLXDisplayConfig { } static std::shared_ptr create() { - static std::weak_ptr instance; - auto shared = instance.lock(); - if (!shared) { - instance = shared = std::make_shared(Key{}); - } - return shared; + // C++11 magic static guarantees thread-safe one-shot initialization. + static const auto instance = std::make_shared(Key{}); + return instance; } public: diff --git a/platform/windows/src/headless_backend_egl.cpp b/platform/windows/src/headless_backend_egl.cpp index 554f9b9043ed..456c47ece5ad 100644 --- a/platform/windows/src/headless_backend_egl.cpp +++ b/platform/windows/src/headless_backend_egl.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -53,12 +54,9 @@ class EGLDisplayConfig { ~EGLDisplayConfig() { eglTerminate(display); } static std::shared_ptr create() { - static std::weak_ptr instance; - auto shared = instance.lock(); - if (!shared) { - instance = shared = std::make_shared(Key{}); - } - return shared; + // C++11 magic static guarantees thread-safe one-shot initialization. + static const auto instance = std::make_shared(Key{}); + return instance; } public: