Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

Smart pointer pitfalls in C++

Common mistakes with unique_ptr and shared_ptr in C++: reference cycles, double ownership, this, function arguments, get(), slicing and unneeded shared_ptr.

Smart pointers remove most manual delete calls, but they have their own failure modes. Nearly all of them come from a small set of mistakes: creating two owners for one object, hiding ownership in a raw pointer, forming cycles, or using the wrong smart pointer for the job.

This page shows the mistakes that cause real bugs, with a safe version of each.

Try it

C++ A shared_ptr cycle that leaks, and the fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Ownership mistakes that compile

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The common mistakes

Mistake Why it hurts Fix
shared_ptr cycles Counts never drop to zero; objects leak Make the back or upward link a weak_ptr
shared_ptr<T>(raw) twice Two control blocks, double delete Create once, then copy the smart pointer
std::shared_ptr<T>(this) Same as above, from inside the class Derive from enable_shared_from_this
Deleting p.get() or wrapping it again Double ownership Treat get() as a borrowed view only
unique_ptr in a function parameter by value when the function just reads Forces callers to give up ownership Take T&, const T& or T*
shared_ptr everywhere "to be safe" Atomic counts, unclear lifetimes, hidden cycles Start with unique_ptr; share only when ownership is truly shared
Using make_shared for large objects with long-lived weak_ptrs Object memory stays allocated until weak pointers go Use shared_ptr<T>(new T) for that case
unique_ptr<Base> with a non-virtual destructor Derived part not destroyed Give Base a virtual destructor
Reset in a destructor that calls back into the owner Re-entrancy surprises Move the pointer to a local first, then reset

Usage notes

  • Unspecified evaluation order in older code like f(std::shared_ptr<A>(new A), g()) could leak if g() throws. Use make_shared.
  • Slicing is not an issue for pointers, but copying the pointee by value (Base b = *p;) does slice.
  • Containers of raw pointers (std::vector<Widget*>) leave ownership unclear. Use vector<unique_ptr<Widget>> for owners.
  • Moved-from state: a moved-from unique_ptr is guaranteed empty. A moved-from shared_ptr is empty too. Using it afterwards is fine only for assigning or comparing with null.
  • Threads: the reference count is atomic, the pointee is not. Two threads calling non-const methods on the same object still need a lock.
  • Incomplete types: unique_ptr<Impl> members need the destructor defined where Impl is complete (the pimpl idiom).
  • Arrays: use unique_ptr<T[]> or shared_ptr<T[]> (C++17), never unique_ptr<T> pointing at new T[n].
  • Don't store shared_ptr in a static or global without thinking about destruction order at program exit.