Skip to content

Smart Pointers and RAII

Manually managing resources (new/delete, fopen/fclose, lock/unlock) is a nightmare for C++ programmers. The RAII (Resource Acquisition Is Initialization) principle tells us to bind resource acquisition to object construction, leave release to destructors, and let scope manage the lifetime for us. In this chapter, we first dive deep into RAII, then master the design philosophy and correct usage of unique_ptr, shared_ptr, and weak_ptr one by one, and finally see how custom deleters and scope guards handle more complex resource scenarios.

Chapter Contents

Built with VitePress