[BUG] Potential memory leak when registering a benchmark with a lambda
Describe the bug clang analyzer / clang-tidy 13 reports "Potential memory leak" when registering a benchmark with a lambda.
include/benchmark/benchmark.h:1193:3: error: Potential memory leak [clang-analyzer-cplusplus.NewDeleteLeaks,-warnings-as-errors]
return internal::RegisterBenchmarkInternal(
^
bench.cpp:169:13: note: Calling 'RegisterBenchmark<(lambda at bench.cpp:169:71)>'
RegisterBenchmark("name", [&b](State& state) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/benchmark/benchmark.h:1194:7: note: Memory is allocated
::new BenchType(name, std::forward<Lambda>(fn)));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/benchmark/benchmark.h:1193:3: note: Potential memory leak
return internal::RegisterBenchmarkInternal(
^
1 warning treated as error
I confirmed that clang-tidy with "clang-analyzer-*" checks enabled was recently added to this project but it didn't report any issues.
There is no easy way to suppress the warning because it would need to be done in the benchmark.h header.
i guess none of our CI tests register with a lambda. we could run clang-tidy with -clang-analyzer-cplusplus.NewDeleteLeaks as a patch.
also, all our registration functions and macros new the benchmark that's registered, not just the lambda one, so i am a bit surprised it's showing up.
also i don't think it is a leak... BenchmarkFamilies (which holds the registered benchmarks) stores a vector of unique_ptrs. those should be released when the static destructor is called during shutdown.