diff --git a/src/Application.cpp b/src/Application.cpp index 94f38dc54..d213f16fa 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -124,18 +124,23 @@ SeventvEventAPI *makeSeventvEventAPI(Settings &settings) const QString TWITCH_PUBSUB_URL = "wss://pubsub-edge.twitch.tv"; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +IApplication *INSTANCE = nullptr; + } // namespace namespace chatterino { static std::atomic isAppInitialized{false}; -Application *Application::instance = nullptr; -IApplication *IApplication::instance = nullptr; - IApplication::IApplication() { - IApplication::instance = this; + INSTANCE = this; +} + +IApplication::~IApplication() +{ + INSTANCE = nullptr; } // this class is responsible for handling the workflow of Chatterino @@ -182,8 +187,6 @@ Application::Application(Settings &_settings, const Paths &paths, #endif , updates(_updates) { - Application::instance = this; - // We can safely ignore this signal's connection since the Application will always // be destroyed after fonts std::ignore = this->fonts->fontChanged.connect([this]() { @@ -193,7 +196,8 @@ Application::Application(Settings &_settings, const Paths &paths, Application::~Application() { - Application::instance = nullptr; + // we do this early to ensure getApp isn't used in any dtors + INSTANCE = nullptr; } void Application::initialize(Settings &settings, const Paths &paths) @@ -528,6 +532,13 @@ PluginController *Application::getPlugins() } #endif +Updates &Application::getUpdates() +{ + assertInGuiThread(); + + return this->updates; +} + ITwitchIrcServer *Application::getTwitch() { assertInGuiThread(); @@ -1180,9 +1191,9 @@ void Application::initSeventvEventAPI() IApplication *getApp() { - assert(IApplication::instance != nullptr); + assert(INSTANCE != nullptr); - return IApplication::instance; + return INSTANCE; } } // namespace chatterino diff --git a/src/Application.hpp b/src/Application.hpp index 7afaf27f6..09dc38e2f 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -1,6 +1,5 @@ #pragma once -#include "debug/AssertInGuiThread.hpp" #include "singletons/NativeMessaging.hpp" #include @@ -62,9 +61,12 @@ class IApplication { public: IApplication(); - virtual ~IApplication() = default; + virtual ~IApplication(); - static IApplication *instance; + IApplication(const IApplication &) = delete; + IApplication(IApplication &&) = delete; + IApplication &operator=(const IApplication &) = delete; + IApplication &operator=(IApplication &&) = delete; virtual bool isTest() const = 0; @@ -115,8 +117,6 @@ class Application : public IApplication char **argv_{}; public: - static Application *instance; - Application(Settings &_settings, const Paths &paths, const Args &_args, Updates &_updates); ~Application() override; @@ -210,12 +210,7 @@ public: #ifdef CHATTERINO_HAVE_PLUGINS PluginController *getPlugins() override; #endif - Updates &getUpdates() override - { - assertInGuiThread(); - - return this->updates; - } + Updates &getUpdates() override; BttvEmotes *getBttvEmotes() override; BttvLiveUpdates *getBttvLiveUpdates() override; @@ -232,7 +227,7 @@ private: void initSeventvEventAPI(); void initNm(const Paths &paths); - NativeMessagingServer nmServer{}; + NativeMessagingServer nmServer; Updates &updates; };