SEH exception when using googlemock
Asked Answered
M

3

9

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]

Mordy answered 11/4, 2013 at 15:47 Comment(9)
Is this the actual code that generates that output? I see no ProductionTest.CallTheUseOnProductionUser() and two ProductionTest.CallTheMethodUnderTest()s.Sorn
@Sorn It's there. TEST(ProductionTest, CallTheMethodUnderTest) is a macro specifying the unit test which is automatically called buy the testing framework.Strom
I see that test listed twice and no sign of CallTheUseOnProductionUser. Admittedly, I'm not familiar with GoogleMock, but I have used several other test frameworks. Am I missing something?Sorn
@Sorn The test is only being run once. The lines beginning with [RUN] indicate the test being executed and the lines beginning with [OK] or [FAILED] are the results of the test.Strom
The example you provided appears to work fine. I did have to correct the name of one of the tests (which I updated in your question) in order for it compile but that's it.Strom
@metal, there was inconsistency between the code posted and the console output, my attempt to make more readable before posting, which I have cleaned up now.Mordy
@CaptainObvlious If its working for you and I am still getting the exception, then maybe its compiler settings. I am building x64....Mordy
You might try taking out the Google Mock and Test code to see if it works. Replace them with assert() calls and try-catch blocks.Sorn
I have found the issue in my test program. I have googlemock built with _SECURE_SCL=0, to match the code base I am trying to use it on for real. Compiling this with the same setting, and it runs OK. Great for this test program but it means the SEH expection raised on my real code is still out there :(Mordy
W
5

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

Winglet answered 12/2, 2016 at 13:59 Comment(0)
P
3

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.

Petta answered 10/12, 2014 at 7:58 Comment(2)
I'm seeing a similar issue although it's specifically with gtest. My project builds, links, and runs in Release mode just fine, but not in debug mode. I'm not sure what the issue is though as I have built gtest in both modes and CMake finds the appropriate gtest library depending on my build type. Any more comments would greatly help.Bulldog
I had this before too. Not only the same configuration, but make sure it's linking the same C/C++ library (/MT /MD /MTd etc... Visual Studio flags)Cassella
H
2

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

Hiramhirasuna answered 11/9, 2018 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.