GoogleTest: Mock object should be deleted but never is.
-
Hi,
it's the first time to work with GoogleTest. I created a few Mock-Objects, but I don't know how I can delete it.Error message for each (19) Mock-Object in opcNodeList is: Mock object should be deleted but never is.
Last error message: ERROR: 19 leaked mock objects found at program exit.Any ideas?
class TestOpcUa : public Test { public: TestOpcUa() { _selectedConfValid = new MockOpcConf(); ON_CALL(*_selectedConfValid, getSERVERNAME()).WillByDefault(Return(END_POINT_VALID)); ON_CALL(*_selectedConfValid, getOpcNodeList()). WillByDefault(Invoke( [] () { OpcNodeList* opcNodeList = new OpcNodeList(); shared_ptr<MockOpcNode> deviceNo = make_shared<MockOpcNode>(); ON_CALL(*deviceNo, getDISPLAY_NAME()).WillByDefault(Return("DeviceNo")); ON_CALL(*deviceNo, getDATATYPEID()).WillByDefault(Return(4)); ON_CALL(*deviceNo, getPARENTNODE()).WillByDefault(Return("ns=2;s=TestChannel.SPS.DB115")); ON_CALL(*deviceNo, getCHILDNODE()).WillByDefault(Return("ns=2;s=DBW0")); opcNodeList->push_back(deviceNo); //...push_back more Mock-Objects return opcNodeList; })); _opcUaObjectValid = new OpcUa(_selectedConfValid); } ~TestOpcUa() { delete _selectedConfValid; delete _opcUaObjectValid; } protected: MockOpcConf* _selectedConfValid = nullptr; OpcUa* _opcUaObjectValid = nullptr; } TEST_F(TestOpcUa, firstTest) { //......Tests are valid }
-
Hi,
Why are you creating that object on the heap rather than the stack ?
-
@henrik2016 said in GoogleTest: Mock object should be deleted but never is.:
_selectedConfValid = new MockOpcConf();
Youre still creating it on the heap, why not simply:
TestOpcUa() { MockOpcConf _selectedConfValid; // Allocate on the stack, no need to delete explicetly
-
@henrik2016 said in GoogleTest: Mock object should be deleted but never is.:
The function I would like to test need pointer.
And what is the problem? Just pass it the pointer then:
&_selectedConfValid
-
@jsulm But how can I modify the lambda function?
-
@henrik2016 What exactly do you want to change?
-
@jsulm I create 19 Mock-Objects like this:
shared_ptr<MockOpcNode> deviceNo = make_shared<MockOpcNode>();
At the end of my test, I have to delete all objects. How can I do this?
-
@henrik2016 said in GoogleTest: Mock object should be deleted but never is.:
At the end of my test, I have to delete all objects. How can I do this?
You don't if you do not allocate on the heap (don't use new) - that was the whole point of what @SGaist wrote.
So, why do you do it this way_selectedConfValid = new MockOpcConf();
intead of
MockOpcConf _selectedConfValid;
?
-
@jsulm I already use
MockOpcConf _selectedConfValid;
But I have got the other objects.