EXPECT_CALL of googlemock leads to “unknown file:error: SEH exception with code 0xc0000005 thrown in the test body” [closed]

情到浓时终转凉″ 提交于 2020-01-03 20:13:23

问题


I am novice to googlemock. My current project needs googlemock to use. I have learned from basics of gmock from google help site. But when I have tried to implement the same in my project it threw SEH exception with code 0xc0000005 error. My project has multiple threads, one of the thread call RUN_ALL_TESTS The above code leads to SEH exception

tools: VS2010, windows 7

Try to mock the function of a class

using ::testing::Return;
using ::testing::Test;
using ::testing::NiceMock;

class OsInterfaceCPP
{  

public: 

    OsInterfaceCPP(void){};
    virtual ~OsInterfaceCPP(void){};
    virtual int add_test(int a, int b) = 0;
    int calladdtest(int aa, int bb)
    {
        return add_test(aa,bb);
    }
};  
class OsCPPApis : public OsInterfaceCPP 
{  
public: 
    OsCPPApis(void){};
    virtual ~OsCPPApis(void){};
    virtual int add_test(int aa, int bb)
    {
        return (aa+bb);
    }
};

class MockedOSCPPApis : public  OsCPPApis
{
public:
    MockedOSCPPApis(void){};
    virtual ~MockedOSCPPApis(void){};
    MOCK_METHOD2(add_test, int(int aaa, int bbb));
};


OSapiTestFunc::OSapiTestFunc(void){}

OSapiTestFunc::~OSapiTestFunc(void){}

void OSapiTestFunc::SetUp(){}

void OSapiTestFunc::TearDown(){}

void OSapiTestFunc::RunTests()
{
    int argc=0;
    char **argv = 0;
    ::testing::InitGoogleMock(&argc, argv); 

}

TEST_F(OSapiTestFunc, OS_Test1)
{

    OsCPPApis TestOscppapis;
    MockedOSCPPApis Testmockosapi;
    int a, b;
    a = 2;
    b = 5;
    bool test1var = true;
    EXPECT_CALL(Testmockosapi,add_test(a, b));

    TestOscppapis.add_test(5,3);
}

回答1:


0xC0000005 is a "you accessed memory that doesn't exist". I can't see exactly where in your code this happens right now. But that's definitely what goes wrong. You may want to run with a debugger and see where it thinks it goes wrong.




回答2:


The only point I'd supect to be the reason for a (most probably) NULL pointer access is

void OSapiTestFunc::RunTests()
{
    int argc=0;
    char **argv = 0;
    ::testing::InitGoogleMock(argc, argv); 
}

Usually when this is called from a main() function as intended, at least argv never would be NULL because the first argument always contains the executable name.

UPDATE:
Note my edit how argc is passed! Simply the value not the address!



来源:https://stackoverflow.com/questions/14067885/expect-call-of-googlemock-leads-to-unknown-fileerror-seh-exception-with-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!