benchmark icon indicating copy to clipboard operation
benchmark copied to clipboard

[BUG] Potential memory leak when registering a benchmark with a lambda

Open chfast opened this issue 3 years ago • 1 comments

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.

chfast avatar Jan 13 '22 16:01 chfast

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.

dmah42 avatar Jan 14 '22 16:01 dmah42