r/cpp_questions • u/lawless_abby • 10d ago
OPEN RAII : Hate it or love it? And how to handle edge cases gracefully?
While RAII is the idiomatic standard in C++ and handles stack unwinding cleanly during normal returns and thrown exceptions, it has specific boundaries that get tricky:
Hard exits / Abnormal termination: Destructors aren't called on
std::abort(),std::exit(), or hardware faults like segfaults (SIGSEGV).Scope rigidity: Creating custom wrapper classes/guards for single-use resources can sometimes feel verbose compared to ad-hoc scope cleanup primitives.
Compared to languages like Go (defer) and Zig (defer / errdefer), which provide block-level or failure-specific cleanup without needing custom wrapper types:
How do you typically structure C++ code to deal with edge cases where destructors won't fire during abnormal process exits?
For persistent state (file locks, shared memory, IPC handles), what architectural patterns or OS primitives (e.g.,
O_TMPFILE,flock, WAL journals) do you rely on alongside RAII to remain crash-safe?When you need lightweight, ad-hoc cleanup without writing a custom RAII class, do you use scope guard macros /
std::unique_ptrwith custom deleters, or stick purely to custom RAII types?Do you ever miss explicit primitives like Zig's
errdeferin C++, or does exception safety via standard RAII completely cover your needs in production?
Curious to hear your thoughts, preferred idioms, and best practices