Cloud Service creates unnecessary GlobalContext instances (#4823)

Changes GlobalContext to hide the constructor so this doesn't occur again since it is a singleton.
This commit is contained in:
Paul Johnson
2026-02-03 20:02:19 +00:00
committed by GitHub
parent 40db2bc8ec
commit d8b10fe9f4
3 changed files with 17 additions and 10 deletions

View File

@@ -968,8 +968,8 @@ CloudServiceSyncDialog::cancelClicked()
void
CloudServiceSyncDialog::refreshClicked()
{
double distanceFactor = GlobalContext().useMetricUnits ? 1.0 : MILES_PER_KM;
QString distanceUnits = GlobalContext().useMetricUnits ? tr("km") : tr("mi");
double distanceFactor = GlobalContext::context()->useMetricUnits ? 1.0 : MILES_PER_KM;
QString distanceUnits = GlobalContext::context()->useMetricUnits ? tr("km") : tr("mi");
progressLabel->setText(tr(""));
progressBar->setMinimum(0);

View File

@@ -33,8 +33,7 @@
#include <QMutex>
#include <QWebEngineProfile>
// singleton
static GlobalContext *globalContext = NULL;
static QList<Context*> _contexts;
GlobalContext::GlobalContext()
@@ -44,6 +43,14 @@ GlobalContext::GlobalContext()
readConfig(0); // don't reread user metrics just yet
}
GlobalContext*
GlobalContext::context()
{
// Meyer's singleton pattern
static GlobalContext globalContext; // Guaranteed thread-safe initialization
return &globalContext;
}
void
GlobalContext::notifyConfigChanged(qint32 state)
{
@@ -130,11 +137,6 @@ GlobalContext::userMetricsConfigChanged()
SpecialFields::getInstance().reloadFields();
}
GlobalContext *GlobalContext::context()
{
if (globalContext == NULL) globalContext = new GlobalContext();
return globalContext;
}
bool Context::isValid(Context *p) { return p != NULL &&_contexts.contains(p); }

View File

@@ -82,8 +82,8 @@ class GlobalContext : public QObject
public:
GlobalContext();
static GlobalContext *context();
void notifyConfigChanged(qint32);
// metadata etc
@@ -100,6 +100,11 @@ class GlobalContext : public QObject
signals:
void configChanged(qint32); // for global widgets that aren't athlete specific
private:
// singleton pattern
GlobalContext();
GlobalContext(const GlobalContext&) = delete;
GlobalContext& operator=(const GlobalContext&) = delete;
};
class RideNavigator;