[英]Different behaviour observed when using SEH (structured exception handling)
[英]SEH exception when using googlemock
我開始將 googlemock 與 googletest 一起使用,但遇到了一個我無法弄清楚的 SEH 異常。
錯誤信息是:
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
我已經在 SO 和其他地方閱讀了一些類似的問題,但我還沒有找到這樣一個簡單例子的答案。
即這發生在我的真實代碼中,但我也在下面的非常簡單的示例中重現了錯誤。 我正在使用 MSVC2008 進行構建。
重現錯誤的代碼:
#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));
}
我的控制台測試輸出:
[==========] 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 . . .
我使用自己的主要功能如下:
#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();
}
我猜我在這里犯了一個非常基本的錯誤,誰能看到我哪里出錯了? 謝謝!
[原始編輯以使代碼和控制台輸出匹配]
我認為您可以強制 gtest 不隱藏確切的異常(使用代碼可能會做什么:
::testing::GTEST_FLAG(catch_exceptions) = false;
或來自命令行的相同)然后如果您使用調試器,您可以輕松獲得堆棧。 或者即使你不這樣做,我也希望類似 *nix 的操作系統編寫核心文件
當我將 gmock 編譯為DLL並將其鏈接到另一個項目中時,我遇到了同樣的問題。 經過多次嘗試,我發現原因是:
您必須以相同的配置編譯 gmock 和您的項目!
這意味着你必須在調試(發布)配置中編譯 gmock,如果你想在調試(發布)模式下鏈接它。 如果不是,則
未知文件:錯誤:測試主體中拋出代碼為 0xc0000005 的 SEH 異常。
總是發生。
我希望我的經驗可以幫助您,盡管您可能會在不同的場景中遇到此問題。
我收到此錯誤是因為我取消了對空指針的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.