Foundationপ্রথম নীতি থেকে
LEVEL 5 · Programming Languages

RAII

Resource Acquisition Is Initialization — resource-এর জীবন object-এর জীবনের সাথে বেঁধে দেওয়া; scope শেষ = destructor = release, deterministic।

also: Resource Acquisition Is Initialization, scope-based resource management

{
    std::lock_guard<std::mutex> g(m);   // constructor: lock
    // ... exception ছুঁড়লেও ...
}                                        // destructor: unlock — ভুল হওয়ার পথ নেই
  • GC-এর সাথে মৌলিক পার্থক্য: GC memory-র জন্য, কিন্তু file/socket/lock-এর release timing জানে না — RAII deterministic
  • C++ destructor, Rust Drop trait, Python context manager (with) — একই idea তিন syntax-এ
  • Exception safety এর বাই-প্রোডাক্ট: cleanup path আলাদা করে লেখার প্রশ্নই ওঠে না
  • Ownership মডেলের ভিত্তি: resource-এর মালিক ঠিক এক — move হয়, share হয় explicit