Why does googlemock report a leaked mock when I can see it being deleted?
Asked Answered
C

1

9

I'm trying to add unit tests and mocks to a legacy project. As part of this I partially mocked one of the classes, mocking just the methods that I need in the class I'm currently testing. I then pass this mock in to the class for dependency injection and delete it in the destructor, however when I run the test it complains:

ERROR: this mock object (used in test xxxx) should be deleted but never is. Its address is @0000000004208AD0.
ERROR: 1 leaked mock object found at program exit.

When debugging through the code I can see that delete is being called on the mock object in the destructor, but it's still complaining. If I call delete again at the end of the test I get an SEH exception thrown because I'm trying to delete memory that's already been de-allocated, but if I comment out the delete in the destructor then it works (but this would obviously cause a memory leak)

Calcific answered 9/10, 2015 at 20:24 Comment(0)
C
9

The issue is that I didn't declare the destructor virtual, so in the actual code it was calling the base class destructor and not my mock destructor.

This is probably obvious to some based on my last statement about it working when I deleted the mock in the test, but it took me a while to figure it out until I found this link, so I thought I'd share it on SO as well http://wahz.blogspot.ca/2014/08/google-mock-dammit-gets-me-every-time.html

Calcific answered 9/10, 2015 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.