diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2022-07-12 14:57:24 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-07-12 16:27:00 +0000 |
commit | eb0658df5c647ee2af5b5b4eec90f4b06a471eae (patch) | |
tree | ea29541d09803890acf92879873c0c9b81b157c4 /src | |
parent | ac1ca195885cd42bb2ae7ac9a892c93425082669 (diff) | |
download | mongo-eb0658df5c647ee2af5b5b4eec90f4b06a471eae.tar.gz |
SERVER-67764 simplify globalMetricTree singleton to fix coverity memory leak
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/server_status_metric.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/mongo/db/commands/server_status_metric.cpp b/src/mongo/db/commands/server_status_metric.cpp index 347b514b45d..f2d4705d4ce 100644 --- a/src/mongo/db/commands/server_status_metric.cpp +++ b/src/mongo/db/commands/server_status_metric.cpp @@ -30,10 +30,13 @@ #include "mongo/db/commands/server_status_metric.h" #include <fmt/format.h> +#include <memory> #include "mongo/bson/bsontypes.h" #include "mongo/logv2/log.h" +#include "mongo/util/static_immortal.h" #include "mongo/util/str.h" +#include "mongo/util/synchronized_value.h" #define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand @@ -53,14 +56,11 @@ std::string ServerStatusMetric::_parseLeafName(const std::string& name) { } MetricTree* globalMetricTree(bool create) { - static struct { - stdx::mutex* mutex = new stdx::mutex(); // NOLINT - MetricTree* metricTree = nullptr; - } instance; - stdx::lock_guard lock(*instance.mutex); - if (create && !instance.metricTree) - instance.metricTree = new MetricTree(); - return instance.metricTree; + static StaticImmortal<synchronized_value<std::unique_ptr<MetricTree>>> instance{}; + auto updateGuard = **instance; + if (create && !*updateGuard) + *updateGuard = std::make_unique<MetricTree>(); + return updateGuard->get(); } void MetricTree::add(std::unique_ptr<ServerStatusMetric> metric) { |