Skip to content

Commit

Permalink
Merge pull request #289 from iress/OSS-7-fix-virtual-destructor-windows
Browse files Browse the repository at this point in the history
Call default mock method for virtual destructor
  • Loading branch information
FranckRJ committed Aug 15, 2022
2 parents 9ccbf9a + fa2cd3d commit 9f399c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions include/mockutils/mscpp/VirtualTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ namespace fakeit {
for (unsigned int i = 0; i < size; i++) {
_firstMethod[i] = from.getMethod(i);
}
if (VTUtils::hasVirtualDestructor<C>())
setCookie(dtorCookieIndex, from.getCookie(dtorCookieIndex));
}

VirtualTable() : VirtualTable(buildVTArray()) {
Expand All @@ -215,7 +217,7 @@ namespace fakeit {
C *c = (C *) this;
C &cRef = *c;
auto vt = VirtualTable<C, baseclasses...>::getVTable(cRef);
void *dtorPtr = vt.getCookie(numOfCookies - 1); // read the last cookie
void *dtorPtr = vt.getCookie(dtorCookieIndex);
void(*method)(C *) = reinterpret_cast<void (*)(C *)>(dtorPtr);
method(c);
return 0;
Expand All @@ -230,7 +232,7 @@ namespace fakeit {
void *dtorPtr = union_cast<void *>(&VirtualTable<C, baseclasses...>::dtor);
unsigned int index = VTUtils::getDestructorOffset<C>();
_firstMethod[index] = dtorPtr;
setCookie(numOfCookies - 1, method); // use the last cookie
setCookie(dtorCookieIndex, method);
}

unsigned int getSize() {
Expand All @@ -257,6 +259,7 @@ namespace fakeit {
static_assert(sizeof(unsigned int (SimpleType::*)()) == sizeof(unsigned int (C::*)()),
"Can't mock a type with multiple inheritance or with non-polymorphic base class");
static const unsigned int numOfCookies = 3;
static const unsigned int dtorCookieIndex = numOfCookies - 1; // use the last cookie

static void **buildVTArray() {
int vtSize = VTUtils::getVTSize<C>();
Expand Down
9 changes: 8 additions & 1 deletion tests/dtor_mocking_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct DtorMocking : tpunit::TestFixture
{
DtorMocking() :
TestFixture(
TEST(DtorMocking::mock_virtual_dtor_with_fake), //
TEST(DtorMocking::mock_virtual_dtor_no_mocks),
TEST(DtorMocking::mock_virtual_dtor_with_fake),
TEST(DtorMocking::mock_virtual_dtor_with_when),
TEST(DtorMocking::mock_virtual_dtor_by_assignment),
TEST(DtorMocking::call_dtor_without_delete),
Expand All @@ -36,6 +37,12 @@ struct DtorMocking : tpunit::TestFixture
virtual ~SomeInterface() = default;
};

void mock_virtual_dtor_no_mocks() {
Mock<SomeInterface> mock;
SomeInterface* i = &(mock.get());
delete i;
}

void mock_virtual_dtor_with_fake() {
Mock<SomeInterface> mock;
Fake(Dtor(mock));
Expand Down

0 comments on commit 9f399c3

Please sign in to comment.