I am using VS2005, and C++ for unit testing using google mock.
I had a global free function in unit testing, and I used the following code to mock free function:
NiceMock <MockA> mockObj;
struct IFoo {
virtual A* foo() = 0;
virtual ~IFoo() {}
};
struct FooMock : public IFoo {
FooMock() {}
virtual ~FooMock() {}
MOCK_METHOD0(foo, A*());
};
FooMock fooMock;
// foo() implementation
A* foo() {
return fooMock.foo();
}
In the SetUp() function, I set Expectations on the global object like
EXPECT_CALL(fooMock,foo())
.Times(1)
.WillOnce(Return(&mockObj));
TEST(..., instA) {
// ...
}
and in TearDown(), I delete the global mock object fooMock
virtual TearDown(){
delete &fooMock;
}
When I run the code, I get the following error
Error: Memory Leak in xyz.instA,
also,
0 bytes in 0 Free Blocks.
-61 bytes in -1 Normal Blocks.
68 bytes in 7 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 11025 byte
Total allocations: 50602 bytes.
Can anyone tell me what is happening here? If I don't delete fooMock, I get the error "fooMock should be delete but never is", or Heap corruption detected.
From the error, I can see that somewhere my heap is being mishandled, but I cannot find the point. I have tried to debug it step by step as well.
Some help would be really great! :)
fooMockbecause it does not have dynamic storage.fooMock should be delete but never isorHeap corruption detectedare either not reported due to the shown code (minus the delete) or the error reporter is buggy. - eerorika