SEH exception when using googlemock

stevejpurves picture stevejpurves · Apr 11, 2013 · Viewed 9.3k times · Source

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]

Answer

Xu Chang picture Xu Chang · Dec 10, 2014

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.