Resource = anything that must be acquired and released properly: memory, file handles, sockets, mutexes, DB connections, etc.
Acquisition happens in the constructor, release happens in the destructor.
RAII does not imply reference counting or garbage collection.
It just means: tie the resource’s lifetime to an object’s lifetime.
Constructor acquires (alloc, open, lock).
Destructor releases (free, close, unlock).
Scope boundaries = lifetime boundaries.
Reference counting is just one possible RAII strategy.
std::shared_ptr is the example: it uses RAII + refcount → last owner frees the resource.
But std::unique_ptr is RAII too — no refcount at all, just one owner.
Garbage collection = runtime decides when to reclaim.
RAII = deterministic: cleanup happens immediately when object leaves scope.
RAII turns “manual alloc/free” into “automatic acquire/release at scope exit”, without a collector.
#include <fstream>
void processFile(const std::string& path) {
std::ifstream file(path); // opens in ctor
if (!file) throw std::runtime_error("cannot open");
std::string line;
while (std::getline(file, line)) {
// process...
}
} // file goes out of scope -> destructor closes file
#include <mutex>
std::mutex m;
void criticalSection() {
std::lock_guard<std::mutex> guard(m); // locks in ctor
// safe work
} // guard dtor unlocks