简体   繁体   English

使用 googlemock 时出现 SEH 异常

[英]SEH exception when using googlemock

I am starting to use googlemock with googletest but am getting an SEH exception that I can't figure out.我开始将 googlemock 与 googletest 一起使用,但遇到了一个我无法弄清楚的 SEH 异常。

The error message is:错误信息是:

unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.

I have read some similar questions on SO and elsewhere but am yet to find an answer for such a simple example.我已经在 SO 和其他地方阅读了一些类似的问题,但我还没有找到这样一个简单例子的答案。

ie This is happening on my real code, but I've also reproduced the error on the very simple example below.即这发生在我的真实代码中,但我也在下面的非常简单的示例中重现了错误。 I am building with MSVC2008.我正在使用 MSVC2008 进行构建。

code that reproduces the error:重现错误的代码:

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <iostream>

using testing::Exactly;

class Production
{
public:
    virtual ~Production() {};
    virtual void fn() = 0;
};

class ProductionCode : public Production
{
public:
    virtual ~ProductionCode() {};
    void fn() 
    {
        std::cout << "CALLED ProductionCode::fn" << std::endl;
    }
};

class MockProduction : public Production
{
public:
    virtual ~MockProduction() {};
    MOCK_METHOD0(fn, void());
};

class ProductionUser
{
public:
    void methodUnderTest(Production *p)
    {
        p->fn();
    }
};

TEST(ProductionTest, CallTheProductionFunction) {
    ProductionCode p;

    ASSERT_NO_THROW( p.fn() );
}

TEST(ProductionTest, CallTheMethodUnderTest) {
    Production* p = new ProductionCode;
    ProductionUser u;

    ASSERT_NO_THROW( u.methodUnderTest(p) );

    delete p;
}

TEST(ProductionTest, CallTheMethodUnderTestWithMock) {
    MockProduction m;

    EXPECT_CALL(m, fn())
        .Times(Exactly(1));

    ProductionUser u;
    ASSERT_NO_THROW(u.methodUnderTest(&m));
}

my test output from the console:我的控制台测试输出:

[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ProductionTest
[ RUN      ] ProductionTest.CallTheProductionFunction
CALLED ProductionCode::fn
[       OK ] ProductionTest.CallTheProductionFunction (4 ms)
[ RUN      ] ProductionTest.CallTheMethodUnderTest
CALLED ProductionCode::fn
[       OK ] ProductionTest.CallTheMethodUnderTest (2 ms)
[ RUN      ] ProductionTest.CallTheMethodUnderTestWithMock
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.

[  FAILED  ] ProductionTest.CallTheMethodUnderTestWithMock (0 ms)
[----------] 3 tests from ProductionTest (10 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (13 ms total)
[  PASSED  ] 2 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] ProductionTest.CallTheMethodUnderTestWithMock

 1 FAILED TEST

.\simple.cpp(59): ERROR: this mock object (used in test ProductionTest.CallTheMe
thodUnderTestWithMock) should be deleted but never is. Its address is @000000000
014F800.
ERROR: 1 leaked mock object found at program exit.
Press any key to continue . . .

I am using my own main function as follows:我使用自己的主要功能如下:

#include "gtest/gtest.h"
#include "gmock/gmock.h"

int main(int argc, char** argv) {
    // The following line must be executed to initialize Google Mock
    // (and Google Test) before running the tests.
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

I am guessing that I am making a pretty basic mistake here, Can anyone see where I am going wrong?我猜我在这里犯了一个非常基本的错误,谁能看到我哪里出错了? Thanks!谢谢!

[Original Edited to make code & console output match] [原始编辑以使代码和控制台输出匹配]

I think you could force gtest to don't cloak the exact exception (what might be done using the code:我认为您可以强制 gtest 不隐藏确切的异常(使用代码可能会做什么:

::testing::GTEST_FLAG(catch_exceptions) = false;

or the same from the command line) And if then you use a debugger, you easily get the stack.或来自命令行的相同)然后如果您使用调试器,您可以轻松获得堆栈。 Or even if you don't, I expect *nix-like OS to write core file或者即使你不这样做,我也希望类似 *nix 的操作系统编写核心文件

I met the same problem when I compiled the gmock as DLL and linked it in another project.当我将 gmock 编译为DLL并将其链接到另一个项目中时,我遇到了同样的问题。 After a lot of try, I found the reason is:经过多次尝试,我发现原因是:

You have to compile the gmock and your project in the same configuration!您必须以相同的配置编译 gmock 和您的项目!

That means you have to compile the gmock in DEBUG(RELEASE) configuration, if you want to link it in the DEBUG(RELEASE) mode.这意味着你必须在调试(发布)配置中编译 gmock,如果你想在调试(发布)模式下链接它。 If not, the如果不是,则

unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.未知文件:错误:测试主体中抛出代码为 0xc0000005 的 SEH 异常。

always occurs.总是发生。

I hope my experience could help you, though you may encounter this problem in a different scene.我希望我的经验可以帮助您,尽管您可能会在不同的场景中遇到此问题。

我收到此错误是因为我取消了对空指针的引用。

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

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