31

I understand why this is happening, but I'm stuck trying to resolve it...here is what my code is doing when the error is generated (thus, leading to a crash) when my program exits...

pure virtual method called

SomeClass::~SomeClass()
{
   BaseClassObject->SomePureVirtualMethod(this);
}

void DerivedClass::SomePureVirtualMethod(SomeClass* obj)
{
    //Do stuff to remove obj from a collection
}

I never have a call to new SomeClass but I have a QList<SomeClass*> which I append SomeClass* objects to. The purpose of this destructor in SomeClass is to tell DerivedClass to remove a specific instance of SomeClass from it's collection of QList<SomeClass*>.

So, in a concrete example...

BaseClass = Shape

DerivedClass = Triangle

SomeClass = ShapeProperties which owns a reference to Shape

So, I never have a call to new ShapeProperties but I have a QList<ShapeProperties*> inside of Triangle. The destructor in ShapeProperties is to tell Triangle to remove a specific property of ShapeProperties from it's collection of QList<ShapeProperties*>.

user869525
  • 769
  • 2
  • 12
  • 21

3 Answers3

41

By the time your destructor is called, the destructor of inherited classes has already been called. Within constructors and destructors, the dynamic type of the object can effectively be considered to be the same as the static type. That is, when you call virtual methods from within your constructors/destructors it's not the overriden versions of them that are called.

If SomePureVirtualMethod needs to be called at the destructor, then you will have to call it within the destructor of the class where the actual definition of the method you want is.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Then how do I know which instance of `this` I'm referring to if I call it with in the derived constructor? – user869525 May 22 '12 at 18:29
  • I probably misunderstood this, "If `SomePureVirtualMethod` needs to be called at the destructor, then you will have to call it within the destructor of the class where the actual definition of the method you want is." – user869525 May 22 '12 at 18:40
  • 1
    @user869525: In simple words, your call to `SomePureVirtualMethod` from a destructor won't resolve to the most derived override. If you want to call the most derived override, you have to call it from the destructor of the class that implements such override. – K-ballo May 22 '12 at 18:42
  • Yes, I understand this, but it doesn't resolve my issue since I'm in `SomeClass`, not `BaseClass` nor `DerivedClass`.`SomeClass` owns an instance of `BaseClass` and `DerivedClass` owns a `list` of `SomeClass`. The destructor in `SomeClass` needs to remove an instance of `SomeClass` from the `list` inside of `DerivedClass`. – user869525 May 22 '12 at 19:22
14

When you call the virtual method in the destructor of the Base class SomeClass it calls the method(SomePureVirtualMethod()) of the Base class SomeClass which is a pure virtual method with no definition. And hence the error.

Why does this happen?
The type of this in constructor or destructor is of the type whose constructor or destructor is being called and hence dynamic dispatch doesn't work in constructors and destructors as you would expect it to work in all other functions.

Why does it crash?
Because calling a pure virtual function from constructor or destructor is an Undefined Behavior.

C++03 10.4/6 states

"Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined."

How to avoid it?
Just ensure that you don't call a pure virtual function from constructor or destructor.
Don't call virtual methods in constructor or destructor unless you understand the dynamics involved.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

There is another reason why this might happen, depending on your compiler and system, and that is from a dangling reference. Paul S. R. Chisholm explains the possible state of freed memory:

  • The memory might be marked as deallocated.
  • The memory might be deliberately scrambled.
  • The memory might be reused.
  • The memory might have been left exactly the way it was.

The last is an interesting case. What was the object "exactly the way it was"? In this case, it was an instance of the abstract base class; certainly that's the way the vtbl was left. What happens if we try to call a pure virtual member function for such an object?

"Pure virtual function called".

Dodgie
  • 643
  • 1
  • 10
  • 17