Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash due to the uncertain destroying order of static variables #52

Closed
zjx20 opened this issue Oct 5, 2014 · 1 comment · Fixed by #59
Closed

Crash due to the uncertain destroying order of static variables #52

zjx20 opened this issue Oct 5, 2014 · 1 comment · Fixed by #59

Comments

@zjx20
Copy link
Contributor

zjx20 commented Oct 5, 2014

I had encountered a reproducible crash issue.

My testing environment is mac osx 10.9.5 with xcode 6 and clang-600.0.5.
Here is the test code (copied from README):

#include "benchmark/benchmark.h"

static void BM_StringCreation(benchmark::State& state) {
  while (state.KeepRunning())
    std::string empty_string;
}

// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
  std::string x = "hello";
  while (state.KeepRunning())
    std::string copy(x);
}
BENCHMARK(BM_StringCopy);

int main(int argc, const char** argv) {
//  BENCHMARK(BM_StringCreation);
//  BENCHMARK(BM_StringCopy);
  benchmark::Initialize(&argc, argv);
  benchmark::RunSpecifiedBenchmarks();
  return 0;
}

At the end of the execution for everytime, a crash message shows up:

Reading /proc/self/cputime_ns failed. Using getrusage().
Benchmarking on 4 X 1000 MHz CPUs
2014/10/05-11:41:53
DEBUG: Benchmark           Time(ns)    CPU(ns) Iterations
---------------------------------------------------------
DEBUG: BM_StringCreation         14         28   18115649                                  
DEBUG: BM_StringCopy              9         23   22298993                                  
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
Abort trap: 6

When I move BENCHMARK() lines into main(), the issue disappeared.

I debugged this issue for a few hours, and then I realized that the problem is: the static std::mutex benchmark_mutex has been destroyed before static BenchmarkFamilies instance. The destructor of Benchmark calls BenchmarkFamilies::RemoveBenchmark(), but the mutex already invalid, and then it just crashes.

@zjx20 zjx20 changed the title Crash due to the uncertain destroy order of static variables Crash due to the uncertain destroying order of static variables Oct 5, 2014
zjx20 added a commit to zjx20/benchmark that referenced this issue Oct 5, 2014
@zjx20
Copy link
Contributor Author

zjx20 commented Oct 8, 2014

Update: the issue is link-order related

# crash
g++ -o test_bench test_bench.cpp -Wall -Wextra --std=c++11 -lbenchmark
# ok
g++ -lbenchmark -o test_bench test_bench.cpp -Wall -Wextra --std=c++11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant