简体   繁体   English

使用C ++ 11 std :: condition_variable的Gtest意味着valgrind错误

[英]Gtest with C++11 std::condition_variable implies valgrind errors

If I write a test with google test framework this way: 如果我用这种方式用google测试框架编写测试:

TEST_F( TestFName, TestName )
{
     std::condition_variable cv;
}

It generates a valgrind error. 它会生成valgrind错误。 I run it with --leak-check=full --track-origins=yes options. 我用--leak-check=full --track-origins=yes选项运行它。

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. 很难意识到错误来自condition_variable cv声明。 When I declared it global, the error dissapeared. 当我宣布它是全局的时,错误就消失了。

I am running Valgrind-3.7.0 on a machine with Ubuntu 3.8 x86_64. 我在使用Ubuntu 3.8 x86_64的机器上运行Valgrind-3.7.0。

Did someone else encountered the same issue? 别人遇到过同样的问题吗?

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. 基于阅读源代码,我的猜测是,您的编译环境与用于编译您正在使用的libstdc ++二进制文件的环境不匹配。 Specifically, libstdc++ was compiled without _GTHREAD_USE_COND_INIT_FUNC , and it is defined in your environment. 具体来说,libstdc ++是在没有_GTHREAD_USE_COND_INIT_FUNC情况下_GTHREAD_USE_COND_INIT_FUNC ,它是在您的环境中定义的。

Here is the reason: the header <condition_variable> defines a data member of a type which resolves to pthread_cond_t . 原因如下:标头<condition_variable>定义了一个解析为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. 如果定义了宏__GTHREAD_COND_INIT ,则在头中指定此成员的默认初始化,并在实现文件中默认构造函数。 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. 是否__GTHREAD_COND_INIT被定义由控制_GTHREAD_USE_COND_INIT_FUNC宏。

If in-class member initialization is implemented in G++ the way I think it is, eg 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. 如果按照我认为的方式在G ++中实现类内成员初始化,例如在调用构造函数之前执行类内初始化程序,那么当您的libstdc ++使用_GTHREAD_USE_COND_INIT_FUNC编译时,您看到的效果会发生,而现在它是在你的环境中定义的。 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: 来源参考,如果你想深入挖掘:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM