Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEH exception when using googlemock

Tags:

c++

googlemock

I am starting to use googlemock with googletest but am getting an SEH exception that I can't figure out.

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.

i.e. 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.

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]

like image 223
stevejpurves Avatar asked Apr 11 '13 15:04

stevejpurves


3 Answers

I think you could force gtest to don't cloak the exact exception (what might be done using the code:

::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

like image 158
Alexander Enaldiev Avatar answered Nov 04 '22 16:11

Alexander Enaldiev


I met the same problem when I compiled the gmock as DLL and linked it in another project. After a lot of try, I found the reason is:

You have to compile the gmock and your project in the same configuration!

That means you have to compile the gmock in DEBUG(RELEASE) configuration, if you want to link it in the DEBUG(RELEASE) mode. If not, the

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

always occurs.

I hope my experience could help you, though you may encounter this problem in a different scene.

like image 25
Xu Chang Avatar answered Nov 04 '22 15:11

Xu Chang


I was getting this error because I was dereferencing a null pointer.

like image 2
MakotoE Avatar answered Nov 04 '22 14:11

MakotoE