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);
do_something_with(tmp); // throws
delete tmp; // LEAKED!!
...
}
Recap: Constructors and Destructors¶
Deterministic cleanup: at scope exit
Explicit return
End of scope
Exceptions ⟶ stack unwinding