From 10dc1553720b957da73ca077b4d3a096621384ca Mon Sep 17 00:00:00 2001 From: pleroy Date: Mon, 9 Jun 2014 13:27:47 +0200 Subject: [PATCH] Fix a bug in the destruction of BenchmarkFamilies. --- src/benchmark.cc | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/benchmark.cc b/src/benchmark.cc index eb495486..96072e52 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -343,6 +343,14 @@ BenchmarkFamilies::~BenchmarkFamilies() { int BenchmarkFamilies::AddBenchmark(Benchmark* family) { mutex_lock l(&benchmark_mutex); + // This loop attempts to reuse an entry that was previously removed to avoid + // unncessary growth of the vector. + for (int index = 0; index < families_.size(); ++index) { + if (families_[index] == nullptr) { + families_[index] = family; + return index; + } + } int index = families_.size(); families_.push_back(family); return index; @@ -351,11 +359,8 @@ int BenchmarkFamilies::AddBenchmark(Benchmark* family) { void BenchmarkFamilies::RemoveBenchmark(int index) { mutex_lock l(&benchmark_mutex); families_[index] = NULL; - - // Shrink the vector if convenient. - while (!families_.empty() && families_.back() == NULL) { - families_.pop_back(); - } + // Don't shrink families_ here, we might be called by the destructor of + // BenchmarkFamilies which iterates over the vector. } void BenchmarkFamilies::FindBenchmarks(