Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) pure virtual method called

 

pure virtual method called is a runtime error.

 

 

Below I posted code to reproduce the error, which I adapted from [1]. Also from [1]: 'So what’s the moral of the story? If you ever see the error message pure virtual method called / terminate called without an active exception, check your object lifetimes! You may be trying to call members on a destructing (and thus incomplete) object'.

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppPureVirtualMethodCalled.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-08-04T10:58:45
#
#-------------------------------------------------
QT       += core
QT       -= gui
QMAKE_CXXFLAGS += -std=c++0x
TARGET = CppPureVirtualMethodCalled
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <thread>

struct Base
{
  virtual ~Base() { sleep(1); }
  virtual void DoIt() const = 0;
};

struct Derived : public Base
{
  virtual ~Derived() { }
  virtual void DoIt() const { }
};

void Task(const void* const p)
{
  const Base * const b = reinterpret_cast<const Base*>(p);
  while (1) b->DoIt();
}

int main()
{
  const Base * const b = new Derived();
  std::thread t(Task,b);
  delete b;
}

 

 

 

 

 

Screen output

 

pure virtual method called
terminate called without an active exception
The program has unexpectedly finished.

 

 

 

 

 

Program flow

 

 

The program flow can be seen in the (png) picture above. Not how first the instance is deletedm before the thread starts working with it.

 

 

 

 

 

References

 

  1. Software Architecture blog

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict