Destructor Case Studies: Best Practices for Safe and Efficient Teardown
summary
Destructors must be minimal and correct: prefer best destructor is no destructor by using RAII (unique_ptr, scoped handles) so ownership is explicit, avoid redundant clears and resets, and never declare a trivial destructor because that can suppress move operations. Always join threads before destruction, avoid calling virtual functions from constructors/destructors, catch and swallow exceptions so destructors never throw, and let vector implementations skip individual destroys when T is is_trivially_destructible.
AI-written summary · about
chapters (24)
- 0:00Introduction to destructors
- 1:36Why destructors matter
- 3:47C++17 baseline and resources
- 5:34Destructor definition and properties
- 8:06When destructors are called
- 9:34Standard library destructors
- 12:47Avoid explicit destructors
- 16:26Trivial destructors explained
- 18:59Simplifying destructors
- 22:11Avoid public functions in destructors
- 25:03Raw resources and scoped handles
- 27:15RAII and memory leaks
- 29:03Raw pointers and ownership
- 31:10Thread destructors and joining
- 35:11Virtual destructors explained
- 37:29Order of destruction
- 40:02Avoid virtual functions in destructors
- 44:30Exception safety in destructors
- 48:49Explicit destructors and placement new
- 51:26Custom allocators
- 52:09Vector destructors
- 54:15Destructor traits in C++14
- 56:26Best practices summary
- 59:02Q&A
related talks
39mTemporal Dynamics - Exploring the Intricacies of Object Lifetimes in Modern C++
01h 33mSafe and Sane C++ Types
57m“The Unexceptional Exceptions"
01h 02mBack to Basics: Exceptions
39mTemporal Dynamics - Exploring the Intricacies of Object Lifetimes in Modern C++
55mObject Lifetime in C++: From Start to Finish
54mWriting C++ to Be Read
01h 04m“Sane and Safe C++ Classes”
