Skip to content

Commit 1996912

Browse files
committed
Avoid leak by using a smart pointer
Signed-off-by: Otto Moerbeek <[email protected]>
1 parent bbad601 commit 1996912

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pdns/recursordist/rec_channel_rec.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static map<string, std::function<StatsMap()>> d_getmultimembers;
116116

117117
struct dynmetrics
118118
{
119-
std::atomic<unsigned long>* d_ptr;
119+
std::unique_ptr<std::atomic<unsigned long>> d_ptr;
120120
std::string d_prometheusName;
121121
std::optional<PrometheusMetricType> d_prometheusType;
122122
std::optional<string> d_prometheusDescr;
@@ -183,7 +183,7 @@ std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::stri
183183
auto locked = d_dynmetrics.lock();
184184
auto iter = locked->find(str);
185185
if (iter != locked->end()) {
186-
return iter->second.d_ptr;
186+
return iter->second.d_ptr.get();
187187
}
188188

189189
std::string name = prometheusName.empty() ? getPrometheusName(str) : prometheusName;
@@ -201,9 +201,10 @@ std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::stri
201201
descr = prometheusDescr;
202202
}
203203

204-
auto ret = dynmetrics{new std::atomic<unsigned long>(), std::move(name), metricType, std::move(descr)};
205-
(*locked)[str] = ret;
206-
return ret.d_ptr;
204+
auto ret = dynmetrics{std::make_unique<std::atomic<unsigned long>>(), std::move(name), metricType, std::move(descr)};
205+
auto* ptr = ret.d_ptr.get();
206+
(*locked)[str] = std::move(ret);
207+
return ptr;
207208
}
208209

209210
static std::optional<uint64_t> get(const string& name)

0 commit comments

Comments
 (0)