Skip to content

Commit 72351b2

Browse files
authored
Merge pull request #203 from jerryct/fix_potential_memory_leak_2
Fix potential memory leak 2
2 parents 63a24a7 + b9b82a1 commit 72351b2

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <utility>
5+
6+
namespace prometheus {
7+
namespace detail {
8+
9+
// Remove as soon C++14 can be used.
10+
template <typename T, typename... Args>
11+
std::unique_ptr<T> make_unique(Args&&... args) {
12+
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
13+
}
14+
15+
} // namespace detail
16+
} // namespace prometheus

core/include/prometheus/family.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "prometheus/check_names.h"
1616
#include "prometheus/client_metric.h"
1717
#include "prometheus/collectable.h"
18+
#include "prometheus/detail/future_std.h"
1819
#include "prometheus/metric_family.h"
1920

2021
namespace prometheus {
@@ -162,11 +163,12 @@ T& Family<T>::Add(const std::map<std::string, std::string>& labels,
162163
#endif
163164
return *metrics_iter->second;
164165
} else {
165-
auto metric = new T(std::forward<Args>(args)...);
166-
metrics_.insert(std::make_pair(hash, std::unique_ptr<T>{metric}));
166+
auto metric =
167+
metrics_.insert(std::make_pair(hash, detail::make_unique<T>(args...)));
168+
assert(metric.second);
167169
labels_.insert({hash, labels});
168-
labels_reverse_lookup_.insert({metric, hash});
169-
return *metric;
170+
labels_reverse_lookup_.insert({metric.first->second.get(), hash});
171+
return *(metric.first->second);
170172
}
171173
}
172174

core/tests/family_test.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <gmock/gmock.h>
66

77
#include "prometheus/client_metric.h"
8+
#include "prometheus/detail/future_std.h"
89
#include "prometheus/histogram.h"
910

1011
namespace prometheus {
@@ -63,22 +64,23 @@ TEST(FamilyTest, add_twice) {
6364
ASSERT_EQ(&counter, &counter1);
6465
}
6566

66-
#ifndef NDEBUG
6767
TEST(FamilyTest, should_assert_on_invalid_metric_name) {
6868
auto create_family_with_invalid_name = []() {
69-
new Family<Counter>("", "empty name", {});
69+
return detail::make_unique<Family<Counter>>(
70+
"", "empty name", std::map<std::string, std::string>{});
7071
};
71-
EXPECT_DEATH(create_family_with_invalid_name(), ".*");
72+
EXPECT_DEBUG_DEATH(create_family_with_invalid_name(),
73+
".*Assertion `CheckMetricName.*");
7274
}
7375

7476
TEST(FamilyTest, should_assert_on_invalid_labels) {
7577
Family<Counter> family{"total_requests", "Counts all requests", {}};
7678
auto add_metric_with_invalid_label_name = [&family]() {
7779
family.Add({{"__invalid", "counter1"}});
7880
};
79-
EXPECT_DEATH(add_metric_with_invalid_label_name(), ".*");
81+
EXPECT_DEBUG_DEATH(add_metric_with_invalid_label_name(),
82+
".*Assertion `CheckLabelName.*");
8083
}
81-
#endif
8284

8385
} // namespace
8486
} // namespace prometheus

0 commit comments

Comments
 (0)