...
- that destructor is called (you are deleting the component under test in the test)
- that destructor is virtual (you might be deleting the component but it's its destructor isn't virtual so the child one isn't being called)
- you are deleting the dependency in that destructor.
...
If the mocked object is not a direct dependency of the component under test, it is not responsible for deleting it, so you should delete the mock yourself.
4. You used a mock after it has been deleted
Because of how GoogleTest operates under the hood, setting expectations on a deleted mock will not trigger any segfaults, however, GoogleTest will think that the mock wasn't deleted.
Code Block | ||||
---|---|---|---|---|
| ||||
TEST_F(Fixture1, Test1) {
Mock * ourMock = new Mock; // The mock is created (could also be in the test fixture constructor) at address 0x5634d2e8ca736700
EXPECT_CALL(*ourMock, fun()); // We set expectations on the mock
code->run1(); // We hope this calls ourMock->fun();
delete ourMock; // Here, expectations on ourMock are verified and cleanup is performed. GoogleTest is happy.
EXPECT_CALL(*ourMock, fun()); // OH NO! GoogleTest now again thinks 0x5634d2e8ca736700 is still active
} // after all cleanup, GoogleTest will report "Mock leaked at 0x5634d2e8ca736700 used in Fixture1.Test1", despite it was deleted on line 7.
|
Make sure you don't set EXPECT_CALLs after you delete the mock. Sometimes, they can hide in the test destructor, or in a mock destructor.
SIGSEGV: Segmentation fault
...