Browse Source

Ensured a virtual dtor is called even if the base dtor is not virtual.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/256/merge
Dimitar Dobrev 10 years ago
parent
commit
05a44565bd
  1. 7
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 9
      tests/CSharp/CSharp.Tests.cs
  3. 11
      tests/CSharp/CSharp.cpp
  4. 8
      tests/CSharp/CSharp.h
  5. 5
      tests/Common/Common.cpp
  6. 4
      tests/Common/Common.h

7
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -1694,8 +1694,11 @@ namespace CppSharp.Generators.CSharp @@ -1694,8 +1694,11 @@ namespace CppSharp.Generators.CSharp
{
GenerateClassFinalizer(@class);
// Only root bases need a Dispose
if (ShouldGenerateClassNativeField(@class))
// ensure any virtual dtor in the chain is called
var dtor = @class.Destructors.FirstOrDefault(d => d.Access != AccessSpecifier.Private && d.IsVirtual);
var baseDtor = @class.BaseClass == null ? null :
@class.BaseClass.Destructors.FirstOrDefault(d => !d.IsVirtual);
if (ShouldGenerateClassNativeField(@class) || (dtor != null && baseDtor != null))
GenerateDisposeMethods(@class);
}
}

9
tests/CSharp/CSharp.Tests.cs

@ -495,4 +495,13 @@ public class CSharpTests : GeneratorTestFixture @@ -495,4 +495,13 @@ public class CSharpTests : GeneratorTestFixture
Assert.That(multipleInheritanceFieldOffsets.Own, Is.EqualTo(3));
}
}
[Test]
public void TestVirtualDtorAddedInDerived()
{
using (new VirtualDtorAddedInDerived())
{
}
Assert.IsTrue(VirtualDtorAddedInDerived.DtorCalled);
}
}

11
tests/CSharp/CSharp.cpp

@ -906,3 +906,14 @@ MultipleInheritanceFieldOffsetsPrimaryBase::MultipleInheritanceFieldOffsetsPrima @@ -906,3 +906,14 @@ MultipleInheritanceFieldOffsetsPrimaryBase::MultipleInheritanceFieldOffsetsPrima
MultipleInheritanceFieldOffsets::MultipleInheritanceFieldOffsets() : own(3)
{
}
VirtualDtorAddedInDerived::VirtualDtorAddedInDerived()
{
}
VirtualDtorAddedInDerived::~VirtualDtorAddedInDerived()
{
dtorCalled = true;
}
bool VirtualDtorAddedInDerived::dtorCalled = false;

8
tests/CSharp/CSharp.h

@ -828,3 +828,11 @@ public: @@ -828,3 +828,11 @@ public:
MultipleInheritanceFieldOffsets();
int own;
};
class DLL_API VirtualDtorAddedInDerived : public Foo
{
public:
VirtualDtorAddedInDerived();
virtual ~VirtualDtorAddedInDerived();
static bool dtorCalled;
};

5
tests/Common/Common.cpp

@ -319,6 +319,11 @@ const AbstractFoo& ReturnsAbstractFoo::getFoo() @@ -319,6 +319,11 @@ const AbstractFoo& ReturnsAbstractFoo::getFoo()
return i;
}
Ex2* DerivedException::clone()
{
return 0;
}
void DefaultParameters::Foo(int a, int b)
{
}

4
tests/Common/Common.h

@ -231,7 +231,7 @@ struct DLL_API Exception @@ -231,7 +231,7 @@ struct DLL_API Exception
struct DLL_API DerivedException : public Exception
{
virtual Ex2* clone() override { return 0; }
virtual Ex2* clone() override;
};
// Tests for ambiguous call to native functions with default parameters
@ -920,7 +920,7 @@ class AbstractTemplate @@ -920,7 +920,7 @@ class AbstractTemplate
{
public:
AbstractTemplate();
void abstractFunction() = 0;
virtual void abstractFunction() = 0;
};
template <typename T>

Loading…
Cancel
Save