Fix memory leak

This commit is contained in:
2025-09-02 17:59:51 -04:00
parent 0583a63649
commit 1cd34ef4a9

View File

@@ -668,7 +668,7 @@ struct Metric {
};
struct AggregateCounter {
std::vector<Counter::State *> thread_states;
std::span<Counter::State *const> thread_states;
Counter::State *global_state;
};
@@ -677,7 +677,7 @@ struct Metric {
};
struct AggregateHistogram {
std::vector<Histogram::State *> thread_states;
std::span<Histogram::State *const> thread_states;
Histogram::State *global_state;
size_t bucket_count;
std::span<const double> buckets; // For bucket threshold formatting
@@ -724,26 +724,12 @@ struct Metric {
new (&aggregate_histogram) AggregateHistogram(ah);
}
// Destructor
~RenderInstruction() {
switch (type) {
case InstructionType::CALL_COUNTER_CALLBACK:
counter_callback.~CallCounterCallback();
break;
case InstructionType::CALL_GAUGE_CALLBACK:
gauge_callback.~CallGaugeCallback();
break;
case InstructionType::AGGREGATE_COUNTER:
aggregate_counter.~AggregateCounter();
break;
case InstructionType::AGGREGATE_GAUGE:
aggregate_gauge.~AggregateGauge();
break;
case InstructionType::AGGREGATE_HISTOGRAM:
aggregate_histogram.~AggregateHistogram();
break;
}
}
// Destructor not needed, all instructions are trivially destructible
static_assert(std::is_trivially_destructible_v<CallCounterCallback>);
static_assert(std::is_trivially_destructible_v<CallGaugeCallback>);
static_assert(std::is_trivially_destructible_v<AggregateCounter>);
static_assert(std::is_trivially_destructible_v<AggregateGauge>);
static_assert(std::is_trivially_destructible_v<AggregateHistogram>);
// Copy constructor and assignment
RenderInstruction(const RenderInstruction &other) : type(other.type) {
@@ -769,9 +755,8 @@ struct Metric {
RenderInstruction &operator=(const RenderInstruction &other) {
if (this != &other) {
// Destroy current object
this->~RenderInstruction();
// Reconstruct with new type and data
// All union members are trivially destructible, so no need to call
// destructor. Just reconstruct with new type and data.
type = other.type;
switch (type) {
case InstructionType::CALL_COUNTER_CALLBACK: