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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With