From b66bff38373f18dfe35c5c54d5f04aa29dd6e7b4 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 16 May 2023 18:56:17 -0700 Subject: [PATCH] sonar | Replace global static variables within jni/sonar.cpp with local static. Summary: Replacing global static varaibles with local static. No cost at startup time, and also fixes any potential static initialization order fiasco. Differential Revision: D45900844 fbshipit-source-id: 4ff05cbefe2f0c00199f40b9127e28c355d1819e --- android/src/main/cpp/sonar.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/android/src/main/cpp/sonar.cpp b/android/src/main/cpp/sonar.cpp index 330991202..62ac6fb5e 100644 --- a/android/src/main/cpp/sonar.cpp +++ b/android/src/main/cpp/sonar.cpp @@ -44,8 +44,15 @@ void handleException(const std::exception& e) { __android_log_write(ANDROID_LOG_ERROR, "FLIPPER", message.c_str()); } -std::unique_ptr sonarScheduler; -std::unique_ptr connectionScheduler; +std::unique_ptr& sonarScheduler() { + static std::unique_ptr scheduler; + return scheduler; +} + +std::unique_ptr& connectionScheduler() { + static std::unique_ptr scheduler; + return scheduler; +} class JEventBase : public jni::HybridClass { public: @@ -943,9 +950,9 @@ class JFlipperClient : public jni::HybridClass { const std::string app, const std::string appId, const std::string privateAppDirectory) { - sonarScheduler = + sonarScheduler() = std::make_unique(callbackWorker->eventBase()); - connectionScheduler = + connectionScheduler() = std::make_unique(connectionWorker->eventBase()); FlipperClient::init( @@ -956,8 +963,8 @@ class JFlipperClient : public jni::HybridClass { std::move(app), std::move(appId), std::move(privateAppDirectory)}, - sonarScheduler.get(), - connectionScheduler.get(), + sonarScheduler().get(), + connectionScheduler().get(), insecurePort, securePort, altInsecurePort,