Commit cbcd7b65 by Eric Fiselier

Fix std::cout/std::cerr static initialization order fiasco.

The benchmark library internals write to std::cout/std::cerr during program startup. This can cause segfaults when the user doesn't include <iostream> in the benchmark (which init's the streams). This patch fixes this by emitting a dynamic initializer in every TU which initializes the streams.
parent 78e22f10
...@@ -214,6 +214,10 @@ void UseCharPointer(char const volatile*); ...@@ -214,6 +214,10 @@ void UseCharPointer(char const volatile*);
// registered benchmark. // registered benchmark.
Benchmark* RegisterBenchmarkInternal(Benchmark*); Benchmark* RegisterBenchmarkInternal(Benchmark*);
// Ensure that the standard streams are properly initialized in every TU.
int InitializeStreams();
static int stream_init_anchor = InitializeStreams();
} // end namespace internal } // end namespace internal
......
...@@ -1199,6 +1199,11 @@ Benchmark* RegisterBenchmarkInternal(Benchmark* bench) { ...@@ -1199,6 +1199,11 @@ Benchmark* RegisterBenchmarkInternal(Benchmark* bench) {
return bench; return bench;
} }
int InitializeStreams() {
static std::ios_base::Init init;
return 0;
}
} // end namespace internal } // end namespace internal
void Initialize(int* argc, char** argv) { void Initialize(int* argc, char** argv) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment