简体   繁体   English

Google模拟测试框架出现堆错误

[英]Heap error with Google mock test framework

If you download the latest version of Google Mock (1.7.0) there are project files for VS2005 and 2010! 如果您下载最新版本的Google Mock(1.7.0),则有VS2005和2010的项目文件! The project to test is written in VS2008,so I opened the VS2005 file and converted it for VS2008 and compiled with 要测试的项目是用VS2008编写的,因此我打开了VS2005文件并将其转换为VS2008,并使用

  • Multi-threaded Debug (/MTd) 多线程调试(/ MTd)
  • Dynamic Library (.dll) 动态库(.dll)

In the test solution: 在测试解决方案中:

Project to test: 测试项目:

Configuration type: Dynamic Library (.dll) 配置类型: Dynamic Library (.dll)

Runtime library: Multi-threaded Debug (/MTd) 运行时库: Multi-threaded Debug (/MTd)

UnitTest project: UnitTest项目:

#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

int main(int argc, char **argv) 
{   
    ::testing::InitGoogleMock(&argc, argv); 
    return RUN_ALL_TESTS(); 
}

Additional Library Directories: ..\\..\\gmock\\msvc\\2005\\Debug 其他库目录: ..\\..\\gmock\\msvc\\2005\\Debug

Additional Dependencies: gmock.lib gmock_main.lib 其他依赖项: gmock.lib gmock_main.lib

Runtime library: Multi-threaded Debug (/MTd) 运行时库: Multi-threaded Debug (/MTd)

If I run the UnitTest project I get following error: 如果我运行UnitTest项目,则会出现以下错误:

Windows has triggered a breakpoint in Program_UnitTests.exe.

This may be due to a corruption of the heap, which indicates a bug in Program_UnitTests.exe or any of the DLLs it has loaded.

in xmtx.c: 在xmtx.c中:

_RELIABILITY_CONTRACT
void  __CLRCALL_PURE_OR_CDECL _Mtxunlock(_Rmtx *_Mtx)
    {   /* unlock mutex */
    LeaveCriticalSection(_Mtx);
#ifdef _M_CEE
    System::Threading::Thread::EndThreadAffinity();
#endif
    } // <------- STOPPED HERE
 #endif /* !_MULTI_THREAD */

What is wrong here? 怎么了 Thank you for any help! 感谢您的任何帮助!

Super-short answer: compile googlemock as a static library instead of a dll; 简短的答案:将googlemock编译为静态库而不是dll; that might fix your problem. 那可能会解决您的问题。

Much longer answer: 更长的答案:

Problems like this are typically the result of mismatched compiler settings. 这样的问题通常是编译器设置不匹配的结果。 These kinds of problems are a pain to diagnose, which is a large part of the reason for the following guidance given in the googletest FAQ : 这类问题很难诊断,这是googletest常见问题解答中提供以下指导的很大一部分原因:

If you compile Google Test and your test code using different compiler flags, they may see different definitions of the same class/function/variable (eg due to the use of #if in Google Test). 如果您使用不同的编译器标记来编译Google Test和测试代码,则它们可能会看到相同类/函数/变量的不同定义(例如,由于在Google Test中使用了#if)。 Therefore, for your sanity, we recommend to avoid installing pre-compiled Google Test libraries. 因此,出于您的理智考虑,建议您避免安装预编译的Google Test库。 Instead, each project should compile Google Test itself such that it can be sure that the same flags are used for both Google Test and the tests. 相反,每个项目都应自行编译Google Test,以确保可以对Google Test和测试使用相同的标志。

This applies equally to googlemock. 这同样适用于googlemock。 Basically, they're suggesting that you compile the googletest & googlemock source code alongside your own code to avoid this problem. 基本上,他们建议您将googletest和googlemock源代码与自己的代码一起编译,以避免出现此问题。 They also make this quite easy: check out the fused-src directory of the gmock distribution for a voltron .cc file and the corresponding headers, gmock.h and gtest.h. 它们也使此操作变得非常容易:检出gmock发行版的fused-src目录中的voltron .cc文件以及相应的标头gmock.h和gtest.h。

If you'd like to continue linking against a completely separate library, you'll need to verify that all of the compiler settings match in the VS project. 如果要继续针对完全独立的库进行链接,则需要验证VS项目中的所有编译器设置都匹配。 Basically you'll need to check every single configuration in the properties dialog, paying special attention to the toolset, exceptions, preprocessor definitions, RTTI, etc. 基本上,您需要在属性对话框中检查每个配置,尤其要注意工具集,异常,预处理器定义,RTTI等。

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

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