Skip to content

VERY experimental Nintendo Switch 2 emulator

C++

[android] fix crash due to ctor/dtor ordering for std::random_device being uninitialized when used in other static ctor/dtors (#3806)

lizzie21f9db1c272c
diff --git a/src/common/random.cpp b/src/common/random.cpp
index d6865ee807..d951881cd2 100644
--- a/src/common/random.cpp
+++ b/src/common/random.cpp
@@ -1,19 +1,22 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
+#include <optional>
#include <random>
#include "common/random.h"
-static std::random_device g_random_device;
-
namespace Common::Random {
+ [[nodiscard]] static std::random_device& GetGlobalRandomDevice() noexcept {
+ static std::random_device g_random_device{};
+ return g_random_device;
+ }
[[nodiscard]] u32 Random32(u32 seed) noexcept {
- return g_random_device();
+ return GetGlobalRandomDevice()();
}
[[nodiscard]] u64 Random64(u64 seed) noexcept {
- return g_random_device();
+ return GetGlobalRandomDevice()();
}
[[nodiscard]] std::mt19937 GetMT19937() noexcept {
- return std::mt19937(g_random_device());
+ return std::mt19937(GetGlobalRandomDevice()());
}
}