Gtest with C++11 std::condition_variable implies valgrind errors
c++, c++11, googlemock, googletest, valgrind
Solution
My wild guess, based on reading the source, is that your compilation environment does not match the environment used to compile the libstdc++ binaries you're using. Specifically, libstdc++ was compiled without `_GTHREAD_USE_COND_INIT_FUNC`, and it is defined in your environment.
Here is the reason: the header `<condition_variable>` defines a data member of a type which resolves to `pthread_cond_t`. If the macro `__GTHREAD_COND_INIT` is defined, the default initialization for this member is specified in the header, and the constructor is defaulted in the implementation file. If it's not, it is initialized with a function call in the body of a non-default constructor. Whether `__GTHREAD_COND_INIT` is defined is controlled by the `_GTHREAD_USE_COND_INIT_FUNC` macro.
If in-class member initialization is implemented in G++ the way I think it is, e.g. the in-class initializers are executed before calling the constructor, then the effect you see would occur when your libstdc++ was compiled with `_GTHREAD_USE_COND_INIT_FUNC` undefed, while right now it is defined in your environment somewhere. This means that the header does not provide in-class initialization and the default constructor from the library is used, leading to an uninitialized value.
Source references, in case you want to dig deeper:
- https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/std/condition_variable
- https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B11/condition_variable.cc
- https://github.com/mirrors/gcc/blob/master/libgcc/gthr.h
- https://github.com/mirrors/gcc/blob/master/libgcc/gthr-posix.h
Problem
If I write a test with google test framework this way: ``` TEST_F( TestFName, TestName ) { std::condition_variable cv; } ``` It generates a valgrind error. I run it with `--leak-check=full --track-origins=yes` options. ``` Conditional jump or move depends on uninitialised value(s) ==17215== at 0x4E3DA82: pthread_cond_destroy@@GLIBC_2.3.2 (pthread_cond_destroy.c:35) ... Uninitialised value was created by a stack allocation ==17215== at 0x4551D0: TestFName_TestName_test::TestBody() ``` It was weird to realise that the error was from the `condition_variable cv` declaration. When I declared it global, the error dissapeared. I am running Valgrind-3.7.0 on a machine with Ubuntu 3.8 x86_64. Did someone else encountered the same issue?