CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

The C++/CLI end generates crashing code if redundant destructors are removed

Open ddobrev opened this issue 4 years ago • 1 comments

To reproduce, delete the destructors from DerivedFromSecondaryBaseWithIgnoredVirtualMethod and SecondaryBaseWithIgnoredVirtualMethod, and run all tests. TestCodeGeneration is going to crash because of the line where DerivedFromSecondaryBaseWithIgnoredVirtualMethod is destroyed. The crash occurs only in C++/CLI. The reason is that DerivedFromSecondaryBaseWithIgnoredVirtualMethod's chain only gets one destructor in Foo:

CommonTest::Foo::~Foo()
{
    delete NativePtr;
}

Since NativePtr in this case is of type Foo*, this only calls the destructor of Foo – because it isn't virtual. The latter might be a mistake in the source C++ to begin with. It's highly recommended for destructors to be virtual when polymorphism is involved. So we need to decide whether to fix the test, the generator (by generating a destructor per class) or both.

ddobrev avatar Nov 30 '20 20:11 ddobrev

The latter might be a mistake in the source C++ to begin with.

The latter is a mistake.

It's highly recommended for destructors to be virtual when polymorphism is involved.

delete-expression (C++ standard)

If the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined

So we need to decide whether to fix the test, the generator (by generating a destructor per class) or both.

The class Foo needs to be fixed or DerivedFromSecondaryBaseWithIgnoredVirtualMethod should derive from something else.

josetr avatar Nov 30 '20 20:11 josetr