运行Google测试时出现分段错误

时间:2014-08-08 10:05:44

标签: c++ unit-testing googletest

当我尝试通过模拟接受指向对象的指针作为参数的方法来运行gtest时,我遇到了分段错误。我发现了造成麻烦的模拟方法。

class NvmControllerMockApp : NvmController_API
{ 

 public:

   MOCK_METHOD1(registerAccessor, bool(NVM_Accessor *accessor));
   MOCK_METHOD0(update, void());

}

这是由gtest生成的o / p:

Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MeterTamperAppTest
[ RUN      ] MeterTamperAppTest.NeutralDisturbanceCheck
Segmentation fault (core dumped)

MOCK_METHOD1正在创建分段错误。如果从要测试的文件中排除该方法,那么事情似乎正常。值得注意的是,NVM_Accessor类处理了一些指针。我尝试使用GDB调试错误,以下是分段错误点的回溯消息:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004168d3 in testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith (this=0x67f188, untyped_args=0x7fffffffdca0)
    at ../src/gmock-spec-builders.cc:363
363     this->UntypedDescribeUninterestingCall(untyped_args, &ss);
(gdb) backtrace
#0  0x00000000004168d3 in testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith (this=0x67f188, untyped_args=0x7fffffffdca0)
    at ../src/gmock-spec-builders.cc:363
#1  0x0000000000410fc9 in testing::internal::FunctionMockerBase<bool (NVM_Accessor*)>::InvokeWith(std::tr1::tuple<NVM_Accessor*> const&) (
    this=0x67f188, args=...) at /home/sudeep/GramPower/gmock-1.7.0/include/gmock/gmock-spec-builders.h:1530
#2  0x0000000000410c56 in testing::internal::FunctionMocker<bool (NVM_Accessor*)>::Invoke(NVM_Accessor*) (this=0x67f188, a1=0x67f148)
    at /home/sudeep/GramPower/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:97
#3  0x000000000041076f in NvmControllerMockApp::registerAccessor (this=0x67f180, gmock_a1=0x67f148)
    at /home/sudeep/GramPower/gpos_fw/gpos/apps/nvm_controller/mocks/nvm_controller_mock_app.h:26
#4  0x0000000000413470 in MeterTamperApp::MeterTamperApp (this=0x67f128, env_=0x67ee90) at apps/meter_tamper/meter_tamper_app.cpp:31
#5  0x0000000000410989 in MeterTamperAppMockEnvironment::MeterTamperAppMockEnvironment (this=0x67ee90)
    at apps/meter_tamper/tests/../mocks/meter_tamper_app_mock_environment.h:23
#6  0x0000000000410a3e in MeterTamperAppTest::MeterTamperAppTest (this=0x67ee80) at apps/meter_tamper/tests/meter_tamper_app_dtest.cpp:30
#7  0x0000000000410b10 in MeterTamperAppTest_NeutralDisturbanceCheck_Test::MeterTamperAppTest_NeutralDisturbanceCheck_Test (this=0x67ee80)
    at apps/meter_tamper/tests/meter_tamper_app_dtest.cpp:36

4 个答案:

答案 0 :(得分:1)

也许迟到的答案,但我今天跑到同样的问题。也许分享这些信息会对其他人有帮助吗?

解决方案非常简单:使用当前的git版本。

相关评论以及1.7.0版gmock的错误可以在这里找到:

gcc 6.1.0 segmentation fault - gcc bug?

可以在此处找到google test的错误报告: https://github.com/google/googletest/issues/705

最后一个链接还提供了一个修复程序,可以合并到1.7.0而不检查当前的git repo。

答案 1 :(得分:1)

我遇到了类似的问题 - 模拟类实例化时的分段错误。我将gmock和gtest构建为静态库。 通过将-Dgtest_disable_pthreads=OFF选项传递给cmake解决了该问题。 希望这会有助于其他人。

答案 2 :(得分:0)

可能您的目标文件生成错误。删除所有目标文件并从头开始编译。

答案 3 :(得分:0)

我遇到了同样的问题。 就我而言,这是因为“ EXPECT_EQ”没有中断测试执行:

    std::vector<int> ret = some_call(); //here the empty vector intializing "ret"
    EXPECT_EQ(ret.size(), 1); //here is failure
    EXPECT_EQ(ret[0], expectedResult); //here is segmentation. Author expected test termination one line above

..我将深入研究gtest文档。