首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用googlemock时出现SEH异常

使用googlemock时出现SEH异常
EN

Stack Overflow用户
提问于 2013-04-11 23:47:05
回答 3查看 10K关注 0票数 9

我开始使用googlemock和googletest,但我得到了一个SEH异常,我不知道。

错误消息为:

代码语言:javascript
复制
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.

我在SO和其他地方读到了一些类似的问题,但还没有找到这样一个简单的例子的答案。

也就是说,这是在我的真实代码上发生的,但我也在下面非常简单的示例中重现了错误。我正在使用MSVC2008构建。

重现错误的代码:

代码语言:javascript
复制
#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));
}

我从控制台得到的测试输出:

代码语言:javascript
复制
[==========] 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 . . .

我使用自己的main函数,如下所示:

代码语言:javascript
复制
#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();
}

我猜我在这里犯了一个非常基本的错误,有人能看出我错在哪里吗?谢谢!

原始编辑以使代码和控制台输出匹配

EN

回答 3

Stack Overflow用户

发布于 2016-02-12 21:59:11

我认为您可以强制gtest不掩盖确切的异常(使用以下代码可以做什么:

代码语言:javascript
复制
::testing::GTEST_FLAG(catch_exceptions) = false;

或者在命令行中也是如此),如果使用调试器,则很容易获得堆栈。或者,即使您不这样做,我也希望*nix类操作系统能够写入核心文件

票数 4
EN

Stack Overflow用户

发布于 2014-12-10 15:58:25

当我将gmock编译为DLL并将其链接到另一个项目中时,我遇到了同样的问题。经过多次尝试,我发现原因是:

你必须在相同的配置中编译和你的项目!

这意味着如果您想在调试(发布)模式中链接gmock,则必须在DEBUG(RELEASE)配置中编译gmock。如果不是,则

未知文件:错误:测试正文中抛出代码为0xc0000005的SEH异常。

总是会发生的。

我希望我的经验可以帮助你,尽管你可能会在不同的场景中遇到这个问题。

票数 3
EN

Stack Overflow用户

发布于 2018-09-12 03:48:17

我收到这个错误是因为我正在取消引用一个空指针。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15953255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档