Fix memory leaks
This commit is contained in:
@@ -329,19 +329,23 @@ struct Metric {
|
||||
|
||||
// Global arena allocator for metric families and persistent global state
|
||||
static ArenaAllocator &get_global_arena() {
|
||||
static ArenaAllocator global_arena(64 * 1024); // 64KB initial size
|
||||
return global_arena;
|
||||
static auto *global_arena =
|
||||
new ArenaAllocator(64 * 1024); // 64KB initial size
|
||||
return *global_arena;
|
||||
}
|
||||
|
||||
// Function-local statics to avoid static initialization order fiasco
|
||||
static auto &get_counter_families() {
|
||||
using FamilyMap = std::map<
|
||||
std::string_view, Family<Counter>::State *, std::less<std::string_view>,
|
||||
ArenaStlAllocator<
|
||||
std::pair<const std::string_view, Family<Counter>::State *>>>;
|
||||
static FamilyMap *counterFamilies = new FamilyMap(
|
||||
ArenaStlAllocator<
|
||||
std::pair<const std::string_view, Family<Counter>::State *>>(
|
||||
using FamilyMap =
|
||||
std::map<std::string_view, ArenaAllocator::Ptr<Family<Counter>::State>,
|
||||
std::less<std::string_view>,
|
||||
ArenaStlAllocator<
|
||||
std::pair<const std::string_view,
|
||||
ArenaAllocator::Ptr<Family<Counter>::State>>>>;
|
||||
static FamilyMap *counterFamilies =
|
||||
new FamilyMap(ArenaStlAllocator<
|
||||
std::pair<const std::string_view,
|
||||
ArenaAllocator::Ptr<Family<Counter>::State>>>(
|
||||
&get_global_arena()));
|
||||
return *counterFamilies;
|
||||
}
|
||||
@@ -360,13 +364,16 @@ struct Metric {
|
||||
|
||||
static auto &get_histogram_families() {
|
||||
using FamilyMap =
|
||||
std::map<std::string_view, Family<Histogram>::State *,
|
||||
std::map<std::string_view,
|
||||
ArenaAllocator::Ptr<Family<Histogram>::State>,
|
||||
std::less<std::string_view>,
|
||||
ArenaStlAllocator<std::pair<const std::string_view,
|
||||
Family<Histogram>::State *>>>;
|
||||
static FamilyMap *histogramFamilies = new FamilyMap(
|
||||
ArenaStlAllocator<
|
||||
std::pair<const std::string_view, Family<Histogram>::State *>>(
|
||||
ArenaStlAllocator<
|
||||
std::pair<const std::string_view,
|
||||
ArenaAllocator::Ptr<Family<Histogram>::State>>>>;
|
||||
static FamilyMap *histogramFamilies =
|
||||
new FamilyMap(ArenaStlAllocator<
|
||||
std::pair<const std::string_view,
|
||||
ArenaAllocator::Ptr<Family<Histogram>::State>>>(
|
||||
&get_global_arena()));
|
||||
return *histogramFamilies;
|
||||
}
|
||||
@@ -872,11 +879,7 @@ Family<Counter> create_counter(std::string_view name, std::string_view help) {
|
||||
auto name_view = arena_copy_string(name, global_arena);
|
||||
auto &familyPtr = Metric::get_counter_families()[name_view];
|
||||
if (!familyPtr) {
|
||||
// NOTE: Family<T>::State instances are never destroyed - this is fine
|
||||
// because the number of metric families is bounded by application design
|
||||
familyPtr = new (global_arena.allocate_raw(sizeof(Family<Counter>::State),
|
||||
alignof(Family<Counter>::State)))
|
||||
Family<Counter>::State(global_arena);
|
||||
familyPtr = global_arena.construct<Family<Counter>::State>(global_arena);
|
||||
familyPtr->name = name_view;
|
||||
familyPtr->help = arena_copy_string(help, global_arena);
|
||||
} else {
|
||||
@@ -885,7 +888,7 @@ Family<Counter> create_counter(std::string_view name, std::string_view help) {
|
||||
"metric family already registered with different help text", name);
|
||||
}
|
||||
Family<Counter> family;
|
||||
family.p = familyPtr;
|
||||
family.p = familyPtr.get();
|
||||
return family;
|
||||
}
|
||||
|
||||
@@ -925,9 +928,7 @@ Family<Histogram> create_histogram(std::string_view name, std::string_view help,
|
||||
if (!family_ptr) {
|
||||
// NOTE: Family<T>::State instances are never destroyed - this is fine
|
||||
// because the number of metric families is bounded by application design
|
||||
family_ptr = new (global_arena.allocate_raw(
|
||||
sizeof(Family<Histogram>::State), alignof(Family<Histogram>::State)))
|
||||
Family<Histogram>::State(global_arena);
|
||||
family_ptr = global_arena.construct<Family<Histogram>::State>(global_arena);
|
||||
family_ptr->name = name_view;
|
||||
family_ptr->help = arena_copy_string(help, global_arena);
|
||||
|
||||
@@ -971,7 +972,7 @@ Family<Histogram> create_histogram(std::string_view name, std::string_view help,
|
||||
name);
|
||||
}
|
||||
Family<Histogram> family;
|
||||
family.p = family_ptr;
|
||||
family.p = family_ptr.get();
|
||||
return family;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user