Thuta Learning
C++
AdvancedProgrammingအခြေခံ

RAII & Smart Pointers

C++သင်ခန်းစာ 1635 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • RAII ကို ရှင်းပြနိုင်ဖို့ — constructor မှာ resource ယူပြီး destructor မှာ ပြန်လွှတ်တာ
  • Destructor တွေ ဘယ်အချိန် run မလဲ — ပြောင်းပြန် အစဉ်နဲ့ exception ဖြစ်နေချိန်ပါ — ကြိုမှန်းနိုင်ဖို့
  • std::make_unique နဲ့ heap object ကို ပိုင်ပြီး std::move နဲ့ ownership ကို လွှဲပေးနိုင်ဖို့
  • unique_ptr၊ shared_ptr နဲ့ weak_ptr ထဲက ရွေးချယ်ပြီး shared_ptr cycle ကို ဖြတ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Resource ဆိုတာ သုံးပြီးရင် ပြန်ပေးရမယ့် အရာမှန်သမျှပါ — heap memory၊ ဖွင့်ထားတဲ့ file၊ network connection၊ lock ချထားတဲ့ mutex စသဖြင့်ပါ။ ပြန်မပေးမိရင် leak ဖြစ်ပြီး နှစ်ခါ ပြန်ပေးမိရင် crash ဖြစ်ပါတယ်။ Resource Acquisition Is Initialization ရဲ့ အတိုကောက် RAII က ဒါကို စည်းမျဉ်း တစ်ခုတည်းနဲ့ ဖြေရှင်းပါတယ် — resource ကို destructor က ပြန်ပေးမယ့် object တစ်ခုထဲ ထုပ်ထားပါ။

ကိုယ့်ဘာသာ ပြန်အပ်တဲ့ tag ကပ်ထားတဲ့ စာကြည့်တိုက် စာအုပ်တစ်အုပ်ကို စဉ်းစားကြည့်ပါ — အေးအေးဆေးဆေး ထွက်သွားသည်ဖြစ်စေ၊ မီးလောင် alarm မြည်လို့ ပြေးထွက်သည်ဖြစ်စေ အဆောက်အအုံက ထွက်လိုက်တာနဲ့ စာအုပ်ကို ကိုယ့်ကိုယ်စား ပြန်အပ်ပေးပါတယ်။ C++ မှာ "အဆောက်အအုံက ထွက်တာ" က scope ရဲ့ အဆုံးဖြစ်ပြီး std::unique_ptr လို smart pointer တွေက memory အတွက် အဆင်သင့် လုပ်ထားတဲ့ tag တွေပါ။

Problem → Why → Solution

ပြဿနာ: လက်နဲ့ရေးတဲ့ delete က မကောင်းတဲ့ လမ်းကြောင်းတွေကို လွတ်သွား

Raw new နဲ့ delete သုံးရင် function ကနေ ထွက်တဲ့ လမ်းကြောင်းတိုင်းက သန့်ရှင်းရေး လုပ်ဖို့ မှတ်ထားရပါတယ်။ အောက်က function က report buffer တစ်ခု allocate လုပ်ပြီး text အလွတ်ဆိုရင် စောစော return ပြန်ကာ throw လုပ်နိုင်တဲ့ function တစ်ခုကို ခေါ်ပါတယ်။ အဲဒီ လမ်းကြောင်းတိုင်းက delete ကို ကျော်သွားလို့ request မကောင်းတိုင်း memory နည်းနည်းစီ leak ဖြစ်ပါတယ်။ File လို ဒုတိယ resource တစ်ခု ထပ်ထည့်လိုက်ရင် သန့်ရှင်းရေး လမ်းကြောင်း အရေအတွက် နှစ်ဆ ဖြစ်သွားပါတယ်။ Pointer ကို ဒုတိယ variable ထဲ copy ကူးပြီး နှစ်ခုလုံးကို delete လုပ်တာကတော့ ဆန့်ကျင်ဘက် bug ဖြစ်တဲ့ double delete ဖြစ်ပြီး undefined behaviour ပါ။

report.cpp
#include <stdexcept>
#include <string>

struct Buffer {
    std::string data;
};

void validate(const std::string& text) {
    if (text.size() > 100) throw std::length_error("too long");
}

void saveReport(const std::string& text) {
    Buffer* buffer = new Buffer;
    if (text.empty()) return;          // leak: delete never runs
    validate(text);                    // throws -> leak
    buffer->data = text;
    delete buffer;                     // only the happy path gets here
}

သန့်ရှင်းရေး code ရေးရုံနဲ့ ဘာကြောင့် မလုံလောက်တာလဲ

ပြဿနာက တစ်ခါ မေ့သွားတာ မဟုတ်ပါဘူး — သန့်ရှင်းရေးက resource ယူတဲ့နေရာနဲ့ တခြားနေရာမှာ ရှိနေပြီး early return ဒါမှမဟုတ် exception တိုင်းက အဲဒါကို ထပ်ရေးရမယ့် လမ်းကြောင်းအသစ် ဖန်တီးလို့ပါ။ C++ က scope ကနေ ထွက်တဲ့ လမ်းကြောင်းတိုင်းမှာ — catch လုပ်ထားတဲ့ exception အတွက် stack unwinding အပါအဝင် — အချက်တစ်ချက်ကို အာမခံပါတယ်၊ local object တွေရဲ့ destructor တွေ run ပါတယ်။ RAII က သန့်ရှင်းရေးကို destructor ထဲ ရွှေ့လိုက်လို့ ထပ်မရေးရဘဲ လမ်းကြောင်းအားလုံးမှာ တစ်ကြိမ်တိတိ ဖြစ်ပါတယ်။

Core concept: ownership က သက်တမ်းကို လိုက်တယ်

RAII က object တစ်ခုရဲ့ ဘဝထဲက အချိန် နှစ်ခုကို တွဲထားပါတယ်။ Constructor က resource ကို ယူပြီး ~TypeName() လို့ နာမည်ပေးထားတဲ့ special member function ဖြစ်တဲ့ destructor က ပြန်လွှတ်ပါတယ်။ Local object ရဲ့ destructor က control က သူ့ scope ကနေ ထွက်တဲ့အခါ အလိုအလျောက် run ပါတယ် — ပိတ် brace မှာ၊ return မှာ၊ break မှာ ဒါမှမဟုတ် exception တစ်ခု အပြင်ကို ပျံ့နေချိန်မှာ ဖြစ်ပါတယ်။ Scope တစ်ခုတည်းထဲက object တွေကို တည်ဆောက်တဲ့ အစဉ်ရဲ့ ပြောင်းပြန်နဲ့ ဖျက်လို့ တခြား resource ပေါ်မှာ တည်ဆောက်ထားတဲ့ အရာကို အရင် ဖြုတ်ပါတယ်။

text
Where the destructor runs
-------------------------
 void runReport() {
     auto temp = std::make_unique<Connection>("report");  // open
     ...use temp...                // a return or throw here: same
 }   // <- scope ends: temp is destroyed, Connection closes

Standard library ထဲမှာ RAII type တွေ အပြည့်ပါ။ std::string နဲ့ std::vector က သူတို့ memory ကို free လုပ်ပြီး std::ifstream က file ကို ပိတ်ပါတယ်၊ std::lock_guard က mutex ကို unlock လုပ်ပြီး std::jthread က thread ကို join လုပ်ပါတယ်။ ကိုယ့် class ထဲမှာ ဒီလို member တွေပဲ ရှိရင် ကိုယ်ပိုင် destructor မလိုပါဘူး — သူတို့ destructor တွေက အလုပ်ကို လုပ်ပြီးသားပါ။

Heap ပေါ်က object တစ်ခုအတွက် std::make_unique<T>(args...) နဲ့ ဖန်တီးတဲ့ std::unique_ptr<T> ကို သုံးပါ။ သူက တစ်ဦးတည်းသော ပိုင်ရှင်ပါ — unique_ptr ဖျက်ဆီးခံရတဲ့အခါ object ကို delete လုပ်ပါတယ်။ ပိုင်ရှင် နှစ်ယောက်ဆိုရင် နှစ်ယောက်လုံး delete လုပ်မှာမို့ copy ကူးလို့ မရပေမယ့် std::move နဲ့ ownership ကို ရွှေ့လို့ ရပါတယ်၊ ရွှေ့ပြီးရင် source က အလွတ် (nullptr) ဖြစ်ကြောင်း အာမခံပါတယ်။ ပုံမှန်အားဖြင့် raw pointer နဲ့ size တူပြီး runtime overhead မရှိပါဘူး။

Program ရဲ့ အစိတ်အပိုင်း အများအပြားက object တစ်ခုကို တကယ် အသက်ရှင်အောင် ထိန်းထားဖို့ လိုရင် std::make_shared နဲ့ ဖန်တီးတဲ့ std::shared_ptr<T> ကို သုံးပါ။ သူက reference count ကို ထိန်းထားပြီး နောက်ဆုံး shared_ptr ပျောက်သွားတဲ့အခါ object ကို delete လုပ်ပါတယ်။ std::weak_ptr က shared object ကို မပိုင်ဘဲ ကြည့်ပြီး object ရှိနေသေးမှသာ သူ့ lock() က shared_ptr တစ်ခုကို ပြန်ပေးပါတယ်။ မပိုင်တဲ့ access အတွက် raw pointer နဲ့ reference တွေကို သုံးလို့ ရနေဆဲပါ — modern code ထဲက ပျောက်သွားတာက naked new နဲ့ delete ပါ။

Smart pointerပိုင်ရှင်ဘယ်အချိန် သုံးမလဲ
std::unique_ptr<T>တစ်ယောက်တိတိHeap object အတွက် default
std::shared_ptr<T>အများ၊ reference count နဲ့Ownership ကို တကယ် မျှပိုင်ရတဲ့အခါ
std::weak_ptr<T>မရှိ (observer)Back link၊ cache၊ cycle ဖြတ်ဖို့
Mental model: ထွက်သွားတဲ့အခါ ပိုင်ရှင်က သန့်ရှင်းရေး လုပ်တယ်
  1. Constructor က resource ကို ယူတယ်
  2. Scope ထဲမှာ object က အဲဒါကို ပိုင်တယ်
  3. Scope ပြီးတယ်: return၊ brace ဒါမှမဟုတ် throw
  4. Destructor က တစ်ကြိမ်တိတိ ပြန်လွှတ်တယ်

Resource ကို ပိုင်တဲ့ object ကို နာမည်တပ်ပြနိုင်ရင် အဲဒီ object ရဲ့ destructor က ပြန်လွှတ်ရမယ့် နေရာပါ။

အထွေထွေ ပုံစံ

cpp
struct Resource {
    Resource()  { /* acquire */ }
    ~Resource() { /* release */ }
};

auto one = std::make_unique<Type>(args);        // sole owner
std::unique_ptr<Type> other = std::move(one);   // transfer; one is now empty
auto shared = std::make_shared<Type>(args);     // reference counted
std::weak_ptr<Type> observer = shared;          // does not own
if (auto locked = observer.lock()) { /* the object is still alive */ }

Resource ကို တိုက်ရိုက် စီမံတဲ့ class အတွက်ပဲ destructor ရေးပါ။ တခြားနေရာတိုင်းမှာ resource တွေကို member ဒါမှမဟုတ် smart pointer ထဲ ထည့်ထားပြီး သူတို့ destructor တွေကို အလုပ်လုပ်ခိုင်းပါ။ make_unique နဲ့ make_shared က new ကို အစားထိုးပြီး ဒီ pattern ထဲမှာ delete ကို ဘယ်နေရာမှ မခေါ်ပါဘူး။

Example: ကိုယ့်ဘာသာ ပိတ်သွားတဲ့ connection တွေ

Connection က ဖွင့်တဲ့အခါရော ပိတ်တဲ့အခါရော စာကြောင်းတစ်ကြောင်း print ထုတ်လို့ သက်တမ်းတွေကို မျက်စိနဲ့ မြင်နိုင်ပါတယ်။ runReport() က function ပြီးတာနဲ့ ပိတ်သွားမယ့် ယာယီ connection တစ်ခုကို ပိုင်ပါတယ်၊ ပြီးတော့ main က ဒုတိယ connection တစ်ခု ဖန်တီးပြီး သူ့ ownership ကို တခြား unique_ptr ဆီ ရွှေ့ပါတယ်။ Output စာကြောင်းတိုင်းကို အဲဒါ ဖြစ်စေတဲ့ code နေရာနဲ့ တိုက်ကြည့်ပါ။

connections.cpp
#include <iostream>
#include <memory>
#include <string>
#include <utility>

struct Connection {
    std::string name;

    explicit Connection(std::string n) : name(std::move(n)) {
        std::cout << "open " << name << '\n';
    }
    ~Connection() { std::cout << "close " << name << '\n'; }
};

void runReport() {
    auto temp = std::make_unique<Connection>("report");
    std::cout << "using " << temp->name << '\n';
}

int main() {
    runReport();

    auto db = std::make_unique<Connection>("db");
    std::unique_ptr<Connection> owner = std::move(db);
    std::cout << std::boolalpha << "db is empty: " << (db == nullptr) << '\n';

    std::cout << "end of main\n";
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lines 9-11explicit Connection(std::string n) : name(std::move(n)) {

    Constructor က resource ကို ယူပါတယ် — ဒီမှာတော့ open လို့ print ထုတ်ရုံပါ။ Constructor syntax ကို Constructors lesson မှာ အပြည့်အစုံ လေ့လာပါမယ်၊ အခုအတွက် "နာမည်ကို သိမ်းပြီး print ထုတ်တယ်" လို့ ဖတ်ပါ။

  2. Line 12~Connection() { std::cout << "close " << name << '\n'; }

    Destructor က RAII ရဲ့ ပြန်လွှတ်တဲ့ တစ်ဝက်ပါ။ ဘယ်သူမှ လက်နဲ့ မခေါ်ပါဘူး — ပိုင်တဲ့ object ဖျက်ဆီးခံရတဲ့အခါ C++ က run ပေးပါတယ်။

  3. Lines 15-18auto temp = std::make_unique<Connection>("report");

    make_unique က Connection ကို heap ပေါ်မှာ ဖန်တီးပြီး သူ့ တစ်ဦးတည်းသော ပိုင်ရှင်ကို ပြန်ပေးပါတယ်။ runReport() ပြီးတဲ့အခါ temp ဖျက်ဆီးခံရပြီး Connection ကို ဖျက်လို့ close report က open db ရဲ့ ရှေ့မှာ ပေါ်ပါတယ်။

  4. Lines 24-25std::unique_ptr<Connection> owner = std::move(db);

    unique_ptr ကို copy ကူးလို့ မရပေမယ့် ownership ကို လွှဲပေးလို့ ရပါတယ်။ Move ပြီးရင် db က null ဖြစ်ကြောင်း အာမခံလို့ နောက် line က true လို့ print ထုတ်ပါတယ်။

  5. Lines 27-28std::cout << "end of main\n";

    end of main က အရင် print ထွက်ပြီးမှ main ရဲ့ scope ပြီးကာ owner က db ကို ပိတ်ပါတယ်။ Program က delete ကို ဘယ်တော့မှ မခေါ်ပေမယ့် Connection တိုင်း တစ်ကြိမ်တိတိ ပိတ်သွားပါတယ်။

You should see
open report
using report
close report
open db
db is empty: true
end of main
close db

std::shared_ptr: မျှပိုင်တဲ့ ownership

တစ်ခါတစ်လေ သဘာဝကျတဲ့ ပိုင်ရှင် တစ်ယောက်တည်း မရှိပါဘူး — ဥပမာ editor ထဲက ဖွင့်ထားတဲ့ document အများက သုံးနေတဲ့ ပုံတစ်ပုံဟာ နောက်ဆုံး document ပိတ်တဲ့အထိ load လုပ်ထားရပါမယ်။ std::shared_ptr က သူ့ ပိုင်ရှင်တွေကို ရေတွက်ပါတယ်။ shared_ptr ကို copy ကူးရင် ပိုင်ရှင် တစ်ယောက်တိုးပြီး တစ်ခု ဖျက်ဆီးခံရရင် တစ်ယောက်လျော့ကာ use_count() က လက်ရှိ အရေအတွက်ကို ပြပါတယ်။ Shared ownership မှာ ကုန်ကျစရိတ် ရှိပါတယ် (thread-safe reference count နဲ့ သီးခြား control block) — ဒါကြောင့် default အဖြစ် မဟုတ်ဘဲ ownership တကယ် မျှပိုင်ရတဲ့အခါမှ သုံးပါ။

shared_logo.cpp
#include <iostream>
#include <memory>
#include <string>

int main() {
    auto logo = std::make_shared<std::string>("logo.png");
    std::cout << "owners: " << logo.use_count() << '\n';      // 1

    {
        std::shared_ptr<std::string> header = logo;
        std::shared_ptr<std::string> footer = logo;
        std::cout << "owners: " << logo.use_count() << '\n';  // 3
    }

    std::cout << "owners: " << logo.use_count() << '\n';      // 1
}

Cycle နဲ့ std::weak_ptr

Reference counting မှာ မမြင်နိုင်တဲ့ နေရာ တစ်ခု ရှိပါတယ်။ A က B ကို shared_ptr နဲ့ ကိုင်ထားပြီး B ကလည်း A ကို shared_ptr နဲ့ ကိုင်ထားရင် တစ်ယောက်က တစ်ယောက်ရဲ့ count ကို သုညထက် ကြီးနေအောင် ထိန်းထားလို့ အပြင်က pointer အားလုံး ပျောက်သွားတဲ့နောက်မှာတောင် ဘယ်တစ်ခုမှ ဖျက်ဆီးမခံရပါဘူး။ ဒါက memory leak ဖြစ်ပြီး တိတ်တဆိတ် ဖြစ်ပါတယ်။ ဖြေရှင်းနည်းက ဦးတည်ချက် တစ်ဖက်ကို std::weak_ptr နဲ့ မပိုင်တဲ့ link ဖြစ်အောင် လုပ်တာပါ — ပုံမှန်အားဖြင့် child ကနေ parent ဆီ ပြန်ညွှန်တဲ့ back link ပါ။ weak_ptr က count ကို မထိခိုက်ပါဘူး၊ ယာယီ shared_ptr ရဖို့ lock() ကို ခေါ်ပါ — object ဖျက်ပြီးသား ဆိုရင် အလွတ် ပြန်ရပါမယ်။

cycle.cpp
#include <iostream>
#include <memory>
#include <string>
#include <utility>

struct Node {
    std::string name;
    std::shared_ptr<Node> next;   // owning link
    std::weak_ptr<Node> prev;     // non-owning back link

    explicit Node(std::string n) : name(std::move(n)) {}
    ~Node() { std::cout << "destroy " << name << '\n'; }
};

int main() {
    {
        auto a = std::make_shared<Node>("A");
        auto b = std::make_shared<Node>("B");
        a->next = b;
        b->next = a;   // cycle: A owns B and B owns A
    }
    std::cout << "cycle scope ended\n";   // no "destroy A" or "destroy B"

    {
        auto c = std::make_shared<Node>("C");
        auto d = std::make_shared<Node>("D");
        c->next = d;
        d->prev = c;   // weak back link: does not own C
        if (auto back = d->prev.lock()) {
            std::cout << "D can still reach " << back->name << '\n';
        }
    }
    std::cout << "weak scope ended\n";   // C and D were destroyed above
}

Raw delete ကို smart pointer နဲ့ ဘယ်တော့မှ မရောပါနဲ့

p.get() ပေါ်မှာ delete ကို မခေါ်ပါနဲ့၊ raw pointer တစ်ခုတည်းကနေ smart pointer နှစ်ခု မဖန်တီးပါနဲ့ — std::shared_ptr<T>(raw) ကို နှစ်ခါ ရေးရင် သီးခြား count နှစ်ခုနဲ့ double delete ဖြစ်ပါတယ်။ Object တွေကို make_unique ဒါမှမဟုတ် make_shared နဲ့ ဖန်တီးပြီး အစကတည်းက smart pointer ကို တစ်ဦးတည်းသော ပိုင်ရှင် ဖြစ်ပါစေ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

RAII က C++ resource ကိုင်တွယ်ပုံရဲ့ ကျောရိုးပါ။ Database client library တွေက destructor ထဲမှာ ပိတ်သွားမယ့် connection object တွေကို ပြန်ပေးပါတယ်၊ std::lock_guard နဲ့ std::scoped_lock က multithreaded code ထဲမှာ unlock မေ့တာကို မဖြစ်နိုင်အောင် လုပ်ပေးပါတယ်၊ file wrapper တွေက scope ထွက်တဲ့အခါ flush လုပ်ပြီး ပိတ်ပါတယ်။ Destructor တွေက stack unwinding ဖြစ်နေချိန်မှာလည်း run လို့ finally block မလိုဘဲ C++ code ကို exception-safe ဖြစ်စေတာ ဒါပါပဲ။

Application code ထဲမှာ std::unique_ptr က နေ့စဉ်သုံး smart pointer ပါ — shape အမျိုးမျိုးကို ကိုင်ထားတဲ့ std::vector<std::unique_ptr<Shape>>၊ class ရဲ့ implementation ကို ဖုံးထားတဲ့ pimpl member ဒါမှမဟုတ် std::unique_ptr<Plugin> ကို ပြန်ပေးတဲ့ factory function စသဖြင့်ပါ။ std::shared_ptr ကတော့ cache တွေ၊ observer list တွေနဲ့ callback တစ်ခု run မပြီးမချင်း object ကို အသက်ရှင်အောင် ထိန်းထားရတဲ့ asynchronous code တွေမှာ ပေါ်ပြီး weak_ptr က tree နဲ့ UI hierarchy တွေရဲ့ parent link တွေကို ထောက်ပံ့ပါတယ်။ Team အများအပြားရဲ့ style rule တွေက review လုပ်တဲ့ code ထဲမှာ naked new နဲ့ delete ကို တိုက်ရိုက် တားမြစ်ထားပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. new နဲ့ delete ကို လက်နဲ့ ရေးတာ

auto* p = new Report; ... delete p; က line နှစ်ကြောင်းကြားက early return ဒါမှမဟုတ် exception တိုင်းမှာ leak ဖြစ်ပါတယ်။ auto p = std::make_unique<Report>(); လို့ ရေးပြီး ဘာကိုမှ delete မလုပ်ပါနဲ့။

2. unique_ptr ကို copy ကူးဖို့ ကြိုးစားတာ

std::unique_ptr<int> p2 = p1; က use of deleted function 'std::unique_ptr<...>::unique_ptr(const std::unique_ptr<...>&)' error နဲ့ ကျပါတယ်။ ဒါက ပိုင်ရှင် နှစ်ယောက် မဖြစ်အောင် type က ကာကွယ်ပေးတာပါ။ Ownership လွှဲဖို့ std::move(p1) ကို သုံးပါ၊ ကြည့်ရုံပဲ လိုတဲ့ code ဆီ reference ဒါမှမဟုတ် raw pointer (p1.get()) ကို ပို့ပါ။

3. shared_ptr ကို နေရာတိုင်း သုံးတာ

Pointer တိုင်းကို shared_ptr လုပ်လိုက်ရင် ဘယ်သူ ဘာကို ပိုင်လဲ ဖုံးကွယ်သွားပြီး copy တိုင်းမှာ reference count ကုန်ကျကာ cycle တွေကိုပါ ဖိတ်ခေါ်ပါတယ်။ unique_ptr နဲ့ စပါ၊ ပိုင်ရှင် နှစ်ယောက်က တစ်ယောက်ထက်တစ်ယောက် တကယ် အသက်ရှည်ဖို့ လိုမှသာ shared_ptr ကို ပြောင်းပါ။

4. Ownership cycle ဖြစ်အောင် လုပ်မိတာ

တစ်ယောက်ကို တစ်ယောက် shared_ptr နဲ့ ကိုင်ထားတဲ့ parent နဲ့ child က ဘယ်တော့မှ ဖျက်ဆီးမခံရပါဘူး။ Back link ကို weak_ptr ဖြစ်အောင် လုပ်ပါ (parent က child ထက် အသက်ရှည်ကြောင်း သေချာရင် raw pointer လည်း ရပါတယ်)။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Resource တိုင်းကို ပိုင်ရှင် object တစ်ခု ပေးပါ၊ အဲဒီ ပိုင်ရှင်ရဲ့ destructor က ပြန်လွှတ်တဲ့ တစ်နေရာတည်း ဖြစ်ပါစေ။
  • Heap object တွေကို naked new နဲ့ မဟုတ်ဘဲ std::make_unique (ဒါမှမဟုတ် std::make_shared) နဲ့ ဖန်တီးပါ။
  • Object ကို သုံးရုံပဲ လုပ်တဲ့ function ဆီ T& ဒါမှမဟုတ် T* ကို ပို့ပါ၊ std::unique_ptr ကို value နဲ့ ပို့တာကို ownership လွှဲဖို့ပဲ သုံးပါ။
  • Destructor တွေကို တိုတိုရှင်းရှင်း ထားပြီး ဘယ်တော့မှ throw မလုပ်ပါစေနဲ့။

Mini challenge

Mini challenge: အမြဲ ပြီးဆုံးတဲ့ step တွေ

Step တွေ စတဲ့အခါနဲ့ ပြီးတဲ့အခါ print ထုတ်တဲ့ job runner အသေးလေး တစ်ခု တည်ဆောက်ပါ။ Job က တစ်ဝက်မှာ fail ဖြစ်ရင်တောင် RAII က step တိုင်းကို ပြီးဆုံးစေကြောင်း သက်သေပြပြီး heap step တစ်ခုကို ownership ယူတဲ့ function ဆီ လွှဲပေးပါ။

  • struct Step: constructor က "start <name>" ကို၊ destructor က "finish <name>" ကို print ထုတ်ရမယ်
  • void runJob(bool diskFull) က download နဲ့ unzip step တွေ ဖန်တီးပြီး diskFull က true ဆိုရင် std::runtime_error("disk full") ကို throw လုပ်ရမယ်
  • main က job ကို ပုံမှန် တစ်ခါ၊ try/catch ထဲမှာ တစ်ခါ run ပြီး "caught: " နဲ့ message ကို print ထုတ်ရမယ်
  • std::make_unique နဲ့ upload step ဖန်တီးပြီး void consume(std::unique_ptr<Step> step) ဆီ ပို့ကာ main က ပိုင်နေသေးလား print ထုတ်ရမယ်
job_runner.cpp
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>

struct Step {
    std::string name;
    // TODO: constructor that stores the name and prints "start <name>"
    // TODO: destructor that prints "finish <name>"
};

void runJob(bool diskFull) {
    // TODO: two local Steps; throw when diskFull, otherwise print "job done"
}

void consume(std::unique_ptr<Step> step) {
    // TODO: print "consuming <name>"; the step finishes when this returns
}

int main() {
    // TODO: runJob(false), then runJob(true) inside try/catch
    // TODO: make_unique an "upload" step, move it into consume(), then check it
}

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: start download start unzip job done finish unzip finish download start download start unzip finish unzip finish download caught: disk full start upload consuming upload finish upload upload still owned here: false

အရိပ်အမြွက် ကြည့်ရန်

runJob() ထဲမှာ throw လုပ်ရင်တောင် catch block မ run ခင် download နဲ့ unzip ကို ပြောင်းပြန် အစဉ်နဲ့ ဖျက်ပါတယ်။ consume() က parameter ကို value နဲ့ ယူလို့ std::move(upload) လိုအပ်ပြီး ပြီးနောက် upload က nullptr ဖြစ်ကြောင်း အာမခံပါတယ်။

အဖြေ ကြည့်ရန်
job_runner.cpp
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>

struct Step {
    std::string name;

    explicit Step(std::string n) : name(std::move(n)) {
        std::cout << "start " << name << '\n';
    }
    ~Step() { std::cout << "finish " << name << '\n'; }
};

void runJob(bool diskFull) {
    Step download("download");
    Step unzip("unzip");
    if (diskFull) {
        throw std::runtime_error("disk full");
    }
    std::cout << "job done\n";
}

void consume(std::unique_ptr<Step> step) {
    std::cout << "consuming " << step->name << '\n';
}

int main() {
    runJob(false);

    try {
        runJob(true);
    } catch (const std::exception& e) {
        std::cout << "caught: " << e.what() << '\n';
    }

    auto upload = std::make_unique<Step>("upload");
    consume(std::move(upload));
    std::cout << std::boolalpha << "upload still owned here: "
              << (upload != nullptr) << '\n';
}

Quick Quiz 1 / 3

Scope တစ်ခုထဲမှာ Logger log; ကို အရင် ဖန်တီးပြီး Database db; ကို နောက်မှ ဖန်တီးပါတယ်။ Scope ပြီးတဲ့အခါ destructor တွေ ဘယ်အစဉ်နဲ့ run မလဲ။

Quick Quiz 2 / 3

auto p1 = std::make_unique<int>(5); std::unique_ptr<int> p2 = p1; မှာ ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

Object နှစ်ခုက တစ်ခုကို တစ်ခု std::shared_ptr နဲ့ ကိုင်ထားပြီး သူတို့ဆီ ညွှန်တဲ့ တခြား pointer အားလုံး ပျောက်သွားပါပြီ။ ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • RAII: constructor က resource ယူပြီး destructor က scope ကနေ ထွက်တဲ့ လမ်းကြောင်းတိုင်းမှာ ပြန်လွှတ်ပါတယ်။
  • Local object တွေကို exception unwinding ဖြစ်ချိန် အပါအဝင် ပြောင်းပြန် အစဉ်နဲ့ အလိုအလျောက် ဖျက်ပါတယ်။
  • std::unique_ptr က heap object ရဲ့ တစ်ဦးတည်းသော ပိုင်ရှင်ပါ — make_unique နဲ့ ဖန်တီးပြီး std::move နဲ့ လွှဲပါ။
  • std::shared_ptr ကို တကယ့် shared ownership အတွက်ပဲ သုံးပြီး cycle ဖြစ်စေမယ့် back link တွေအတွက် std::weak_ptr ကို သုံးပါ။

C++ Core Guidelines — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

RAII & Smart Pointers | Thuta Learning