Skip to content

Commit

Permalink
Fix leak in test, and provide path to remove leak from library (#1169)
Browse files Browse the repository at this point in the history
* Fix leak in test, and provide path to remove leak from library

* make doc change
  • Loading branch information
dominichamon authored and dmah42 committed Jun 3, 2021
1 parent 4175f7a commit 49d9250
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ int main(int argc, char** argv) {
benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input);
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
}
```

Expand Down
3 changes: 3 additions & 0 deletions include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ BENCHMARK(BM_StringCopy);
int main(int argc, char** argv) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
Expand Down Expand Up @@ -274,6 +275,7 @@ class BenchmarkReporter;
class MemoryManager;

void Initialize(int* argc, char** argv);
void Shutdown();

// Report to stdout all arguments in 'argv' as unrecognized except the first.
// Returns true there is at least on unrecognized argument (i.e. 'argc' > 1).
Expand Down Expand Up @@ -1314,6 +1316,7 @@ class Fixture : public internal::Benchmark {
::benchmark::Initialize(&argc, argv); \
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
::benchmark::RunSpecifiedBenchmarks(); \
::benchmark::Shutdown(); \
return 0; \
} \
int main(int, char**)
Expand Down
4 changes: 4 additions & 0 deletions src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ void Initialize(int* argc, char** argv) {
internal::LogLevel() = FLAGS_v;
}

void Shutdown() {
delete internal::global_context;
}

bool ReportUnrecognizedArguments(int argc, char** argv) {
for (int i = 1; i < argc; ++i) {
fprintf(stderr, "%s: error: unrecognized command-line flag: %s\n", argv[0],
Expand Down
2 changes: 2 additions & 0 deletions test/benchmark_gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ TEST(AddCustomContext, Simple) {
testing::UnorderedElementsAre(testing::Pair("foo", "bar"),
testing::Pair("baz", "qux")));

delete global_context;
global_context = nullptr;
}

Expand All @@ -155,6 +156,7 @@ TEST(AddCustomContext, DuplicateKey) {
EXPECT_THAT(*global_context,
testing::UnorderedElementsAre(testing::Pair("foo", "bar")));

delete global_context;
global_context = nullptr;
}

Expand Down

0 comments on commit 49d9250

Please # to comment.