Thursday, April 7, 2011

Google Mock: Leaking Mocks

There are a couple of ways to deal with Mocks that leak. An error will print like the following:
1>.\Tests.cpp(76): ERROR: this mock object (used in test Tests.SampleTest) should be deleted but never is. Its address is @00E659C8.
1>ERROR: 1 leaked mock object found at program exit.
1>Project : error PRJ0019: A tool returned an error code from "Performing Post-Build Event..."
This is an indication of either not calling delete on a pointer that was newed previously, or if the pointer is being deleted, an exception could be being thrown in the destructor of the Mocked class.


The first way is to set the command line argument to include
--gmock_catch_leaked_mocks=0
In our project, we have the test executable running after the project is built so this can just be added to the end of the Post-Build Event in the project properties.

The second way is to say in the test that the specific Mock that you are using can allow leaks.
using ::testing::Mock;
... 
Mock::AllowLeak(mockThatLeaks); 
If your problem is that an Exception is being thrown in the destructor, it should be determined why the Exception is being thrown. It could be that it's just because the class isn't initialized properly in the test or that there really is a problem that should be looked into and fixed.

No comments:

Post a Comment