Introduction

Why Smart Pointers?

Most prominent pointer (memory management) related bugs

  • Memory leak

  • Double free

Even more so with exceptions

  • Alternate return path

  • Requires extra handling for resource cleanup

void do_something() {
    MyClass* tmp = new MyClass(666);
    tmp->do_something(); // throws
    delete tmp;     // LEAKED!!
    ...
}

Recap: Constructors and Destructors

Deterministic cleanup: at scope exit

  • Explicit return

  • End of scope

  • Exceptions ⟶ stack unwinding

../../../../../_images/introduction-dtors.svg