C++核心準則R.1: 使用資源句柄自動管理資源並RAII(資源獲取即初始化)

R.1: Manage resources automatically using resource handles and RAII (Resource Acquisition Is Initialization)
R.1: 使用資源句柄自動管理資源並RAII(資源獲取即初始化)

 

Reason(原因)

To avoid leaks and the complexity of manual resource management. C++'s language-enforced constructor/destructor symmetry mirrors the symmetry inherent in resource acquire/release function pairs such as fopen/fclose, lock/unlock, and new/delete. Whenever you deal with a resource that needs paired acquire/release function calls, encapsulate that resource in an object that enforces pairing for you -- acquire the resource in its constructor, and release it in its destructor.

避免手動管理資源時發生泄露和複雜性。C++語言鼓勵構造函數/析構函數的對稱性映射資源確保/釋放函數對中包含的本質的對稱性。這些函數對包括fopen/fclose,lock/unlock,new/deletre等。無論什麼時候,你處理一個需要成對調用申請/釋放函數時,用一個強制進行成對操作的對象封裝資源--在它的構造函數中申請資源,在它的析構函數中釋放資源。

 

Example, bad(反面示例)

Consider(考慮如下代碼):

void send(X* x, cstring_span destination)
{
    auto port = open_port(destination);
    my_mutex.lock();
    // ...
    send(port, x);
    // ...
    my_mutex.unlock();
    close_port(port);
    delete x;
}

In this code, you have to remember to unlock, close_port, and delete on all paths, and do each exactly once. Further, if any of the code marked ... throws an exception, then x is leaked and my_mutex remains locked.

在這段代碼中,你必須記得在所有路徑上unlock,close_port和delete,而且確切地只做一次。另外,如果任何一處標有...的代碼拋出了異常,那麼x就會泄露,my_mutex會保持鎖定。

 

Example(示例)

Consider(考慮):

void send(unique_ptr<X> x, cstring_span destination)  // x owns the X
{
    Port port{destination};            // port owns the PortHandle
    lock_guard<mutex> guard{my_mutex}; // guard owns the lock
    // ...
    send(port, x);
    // ...
} // automatically unlocks my_mutex and deletes the pointer in x

Now all resource cleanup is automatic, performed once on all paths whether or not there is an exception. As a bonus, the function now advertises that it takes over ownership of the pointer.

現在所有的資源清除都是自動的,在每條路徑上執行一次。無論是否存在異常。作爲額外的獎勵,這個函數對外宣稱它負責資源的所有權。

What is Port? A handy wrapper that encapsulates the resource:

什麼是Port?一個封裝資源的便利的容器。

class Port {
    PortHandle port;
public:
    Port(cstring_span destination) : port{open_port(destination)} { }
    ~Port() { close_port(port); }
    operator PortHandle() { return port; }

    // port handles can't usually be cloned, so disable copying and assignment if necessary
    Port(const Port&) = delete;
    Port& operator=(const Port&) = delete;
};

Note(注意)

Where a resource is "ill-behaved" in that it isn't represented as a class with a destructor, wrap it in a class or use finally

當資源由於沒有表現爲一個帶有虛構函數的類而存在"病態行爲",用一個類封裝它或者使用finally。

 

See also: RAII

參照:RAII

 

參考:

Finally:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Re-finally

RAII:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-raii

 

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r1-manage-resources-automatically-using-resource-handles-and-raii-resource-acquisition-is-initialization

 


 

覺得本文有幫助?歡迎點贊並分享給更多的人。

閱讀更多更新文章,請關注微信公衆號【面向對象思考】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章