RAII (Resource Acquisition Is Initialization) ties a resource's lifetime to an object's lifetime: the resource is acquired in the constructor and released in the destructor. For memory, this means smart pointers or containers own heap memory and free it automatically when they go out of scope—even if an exception is thrown—eliminating manual delete and most leaks.
{
auto p = std::make_unique<int>(5); // acquires
} // destructor frees automatically
Real-world example
Replacing raw new/delete with unique_ptr removes a whole class of leak and double-free bugs.
Likely follow-ups
Why is RAII exception-safe? | What is the destructor's role?
Tools include AddressSanitizer (ASan) and MemorySanitizer for use-after-free, buffer overflows, and uninitialized reads; Valgrind (Memcheck) for leaks and invalid access; LeakSanitizer for leaks; and static analyzers (clang-tidy, cppcheck). They're run in testing/CI to catch memory bugs that don't always crash deterministically.
g++ -fsanitize=address -g prog.cpp && ./a.out
Real-world example
Running the test suite under AddressSanitizer in CI catches a use-after-free before release.
Likely follow-ups
What does AddressSanitizer detect? | Why run these in CI?