14

I am getting a segmentation fault when I try running gtest by mocking a method that accepts pointer to a object as the argument. I identified the mock method that is creating the trouble.

class NvmControllerMockApp : NvmController_API
{ 

 public:

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

}

This is the o/p produced by gtest:

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)

The MOCK_METHOD1 is what is creating the segmentation fault. If that method is excluded from the file that is to be tested then things seem to work fine. As a word of caution the NVM_Accessor class deals with some pointers. I have tried debugging the error using GDB and the following is the backtrace message at the point of segmentation fault :

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
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
user3921737
  • 141
  • 1
  • 1
  • 5
  • Can you share a minimal version of `gmock_main.cc` which rise segmentation fault? – Gluttton Aug 16 '14 at 16:51
  • 2
    It will probably not solve the issue, but the inheritance should be public – BЈовић Sep 09 '14 at 14:47
  • 1
    It would help if you included the test case code for `MeterTamperAppTest.NeutralDisturbanceCheck` as well as the test fixture code, since in the backtrace can be seen that the fixture constructor is involved, which is strange. It means that the segmentation fault is happening before any instruction in the test case is run. – Antonio Pérez Nov 23 '15 at 08:00
  • Did you find out the answer to this problem please @user3921737? – John Jan 21 '16 at 21:20

4 Answers4

4

I had a similar issue - segmentation fault on instantiation of mock classes. I build gmock and gtest as static libraries. The problem has been solved by passing the -Dgtest_disable_pthreads=OFF option to cmake. Hope this will help someone else.

Andrei Smeltsov
  • 528
  • 6
  • 14
2

The solution is quite easy: Use the current git version.

Related comments and what was wrong with the 1.7.0 version of gmock can be found here:

gcc 6.1.0 segmentation fault - gcc bug?

and the bug report for google test can be found here: https://github.com/google/googletest/issues/705

The last link also provides a fix which can be merged into 1.7.0 without checking out the current git repo.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Klaus
  • 24,205
  • 7
  • 58
  • 113
0

Probably your object files were generated wrong. Remove all object files and compile from scratch.

Mateusz Wojtczak
  • 1,621
  • 1
  • 12
  • 28
0

I faced the same issue. In my case this happened because "EXPECT_EQ" is not interrupted test execution:

    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

.. I'm going to dive deep into gtest docs..