2

My service has crashed and i have got this stack trace. I cannot infer anything from here

00007f859cd27834 __gnu_cxx::__verbose_terminate_handler()
@ 00007f859cd25865 __cxxabiv1::__terminate(void (*)())
@ 00007f859cd25892 std::terminate()
@ 00007f859cd263be __cxa_pure_virtual
@ 0000000001996f9f My::Class::~Class()

Can anyone help?

Flovdis
  • 2,945
  • 26
  • 49
user1159517
  • 5,390
  • 8
  • 30
  • 47

2 Answers2

10

This happens if actually a "pure virtual" function was called, which results in a crash.

A pure virtual function is one declared:

virtual void pureVirtualFunction() = 0;

Usually the compiler will detect if you omit to implement a pure virtual function. But there can be situations where it can't.

Call of Pure Virtual Function in Base Constructor

One of the common problems are function calls from the base constructor of the class:

MyClass::MyClass() { 
    pureVirtualFunction(); // Call of pure virtual function from base constructor.
}

Call of Pure Virtual Function in Base Deconstructor

Or a call of a pure virtual method from the deconstructor of a base class:

MyClass::~MyClass() { 
    pureVirtualFunction(); // Call of pure virtual function from base deconstructor.
}

Forward Declarations of Shared Pointers (and alike)

There is another common case if you use forward declarations like this:

class MyClass;
typedef std::shared_ptr<MyClass> MyClassPtr;

The object of a such shared pointer can be destroyed at many places, but the compiler lacks the required information how to call the deconstructor of the class. Make sure you read all warnings of the compiler, it will warn about this problem (and about many other problems with calls of pure virtual methods.)

For this special case make sure you avoid forward declarations of shared pointers and include them only with the class declaration (if possible).

See also this answer.

Community
  • 1
  • 1
Flovdis
  • 2,945
  • 26
  • 49
3

Or more likely:

virtual ~Class () = 0;

This declares a virtual destructor and tells the compiler to make room in the vtable for the destructor. But since it is not defined GCC inserted a call to __cxa_pure_virtual in the vtable.

And when you failed to define a body for ~Class() or failed to explicitly write one in a derived class you are calling __cxa_pure_virtual upon destruction.

You should have received some kind of warning or error when attempting to instantiate an abstract class.

looking a little further up the stack may actually provide more evidence as to what the problem is. And Code samples demonstration how you are using the class would probably be usefull.

Dan
  • 2,460
  • 2
  • 18
  • 12
  • Ah true, it has to be this case. There should be a warning at compile time. A common problem is one is using forward declarations of e.g. `std::shared_ptr`. – Flovdis Apr 05 '14 at 18:45
  • A program with an undefined pure virtual destructor [won't link](http://ideone.com/4YYSOq). – n. m. could be an AI Apr 05 '14 at 19:10