# C++: pure virtual method called — terminate called without an active exception

**Code:** `pure virtual method called` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang, MSVC

A pure virtual function was called on an object that is being constructed or destroyed, where the derived part no longer exists.

## Meaning

During a base-class constructor or destructor the object's dynamic type is the base, so a virtual call resolves to the base version — which is pure and has no body. The same happens when another thread calls a method on an object that is mid-destruction, or after the object has been deleted.

## Common causes

- Calling a virtual function from a base-class constructor or destructor
- Object used after `delete` (dangling pointer)
- Thread calling methods while the object is destroyed
- Slicing a derived object to its base

## Quick fix

1. Don't call virtual functions from constructors and destructors
2. Use a separate `init()` step or a factory function after construction
3. Make the destructor `virtual` and manage lifetime with smart pointers

## Detailed fix by platform

### C++

1. Move the virtual call out of the constructor into a named `start()` method called after construction
2. Use `std::shared_ptr` / `std::weak_ptr` when another thread needs the object

## Code examples

### The trap

```cpp
struct Base {
    Base()  { setup(); }              // calls Base::setup (pure!) during construction
    virtual void setup() = 0;
    virtual ~Base() = default;
};
struct Derived : Base {
    void setup() override {}
};
// Derived d;  -> "pure virtual method called" / terminate

// Fix: call setup() after construction, e.g. Derived d; d.setup();
```

## How to diagnose

1. **Context** — Called from a constructor or destructor?
2. **Lifetime** — Could the object already be deleted?
3. **Threads** — Another thread using it?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/pure-virtual-method-called)
