Beginner Problems - Leaked Mock object but created on the stack?

5,340 views
Skip to first unread message

Mike Long

unread,
Aug 7, 2009, 2:56:07 AM8/7/09
to googl...@googlegroups.com
Hi there,

I am trying to get started with google mock to mock out a fibre optics device in my system.  It looks like this:

class SdhDevice
{
public:

    typedef enum{
        FIBRE1 = 1,
        FIBRE2 = 2
    }Fibre_et;

    // Destructor
    virtual ~SdhDevice();

    // send the heartbeat to the device
    virtual void heartbeat() = 0;

    // get the sdh measurements
    virtual void getMeasurements() = 0;

    // select the preferred SDH fibre
    virtual void selectPreferredFibre(Fibre_et fibre) = 0;

};

The Mock is like this:
SdhDevice::~SdhDevice() {}

class MockSdhDevice : public SdhDevice
{
public:
    MockSdhDevice() { std::cerr << "Constructor" << std::endl;};
    ~MockSdhDevice(){ std::cerr << "Destructor" << std::endl;};
    MOCK_METHOD0(heartbeat, void());
    MOCK_METHOD0(getMeasurements, void());
    MOCK_METHOD1(selectPreferredFibre, void(Fibre_et fibre));
};


An finally the test:
TEST(FibreSelectTestWithMock, testRunCallsHeartbeat)
{

    MockSdhDevice dev;
    EXPECT_CALL(dev, heartbeat());
    EXPECT_CALL(dev, getMeasurements());

    FibreSelector fs(&dev);
    fs.selectFibre();
//    Mock::VerifyAndClearExpectations(&dev);
}

When I try to run this test the destructor is never called (and so the expectations are not checked) and I get this message on the console:


[mlong@n6-ws9 ibn_x86]$ gbuild && bin/unittests --gtest_filter=FibreSelectTestWithMock.testRunCallsHeartbeat
>>OmniWorks Build 6.7C1220 (linux x86)<<
Beginning BUILD processing
n6/test/ibn/apps/fibreselectd/FibreSelectorTestSuite.cpp->n6/test/ibn/apps/fibreselectd/FibreSelectorTestSuite.o
n6/test/unittests.lnk_def->bin/unittests
  n6/test/unittests.lnk_def->n6/test/unittests
Completed BUILD processing
Running main() from gmock_main.cc
Note: Google Test filter = FibreSelectTestWithMock.testRunCallsHeartbeat
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FibreSelectTestWithMock
[ RUN      ] FibreSelectTestWithMock.testRunCallsHeartbeat
Constructor
[       OK ] FibreSelectTestWithMock.testRunCallsHeartbeat
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran.
[  PASSED  ] 1 test.

/homedirs/mlong/work/n6/fibreselectd/src/n6/test/ibn/apps/fibreselectd/FibreSelectorTestSuite.cpp:143: ERROR: this mock object (used in test FibreSelectTestWithMock.testRunCallsHeartbeat) should be deleted but never is. Its address is @0xbfc2c2e8.
ERROR: 1 leaked mock object found at program exit.
[mlong@n6-ws9 ibn_x86]$


It seems the constructor is called but the destructor is never called.  Can anyone see where I've gone wrong?

g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Linux n6-ws9.oslo.eur.slb.com 2.6.18-128.2.1.el5.centos.plus #1 SMP Thu Jul 16 07:24:16 EDT 2009 i686 i686 i386 GNU/Linux

I am using google mock fetched from svn yesterday and built myself.

Thanks for your time,
Mike Long

Mike Long

unread,
Aug 7, 2009, 10:24:30 AM8/7/09
to googl...@googlegroups.com
Hello again,

Please disregard my previous mail, I have found the source of my frustration: a handwritten mock with the same name was linked in the executable.

Thanks for the great framework!

/Mike
Reply all
Reply to author
Forward
0 new messages