Friday, 22 August 2014

Google Mock - Dammit gets me every time

Actually this isn't Google Mock's fault but it does catch a code error.

I have a class

class MyServiceInterface
{
public:
   virtual void provideAService()=0;
   ...
};

Then I have a mock for this

class MockMyService : public MyServiceInterface
{
public:
    MOCK_METHOD0(provideAService, void());
};

In my test I expect it to be called

EXPECT_CALL( *mockService, provideAService());

When the test finishes I get this error. It's weird as I used a smart pointer to hold the pointer to the mock.

c:\work\XXX\test\src\MyServiceTest.cpp(132): ERROR: this mock object should be deleted but never is. Its address is @04334590.
ERROR: 1 leaked mock object found at program exit.

Yes the solution is obvious - the MyServiceInterface has no virtual destructor. The following to the service interface definition and we are all good.

virtual ~MyServiceInterface() { };

It just catches me out often enough that I thought if I write this out I might remember...

No comments:

Post a Comment