原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization

Intent

  • To guarantee release of resource(s) at the end of a scope
  • To provide basic exception safety guarantee

Also Known As

  • Execute-Around Object
  • Resource Release Is Finalization
  • Scope-Bound Resource Management

Motivation

Resources acquired in a function scope should be released before leaving the scope unless the ownership is being transferred to another
scope or object. Quite often it means a pair of function calls - one to acquire a resource and another one to release it. For example, new/delete, malloc/free, acquire/release, file-open/file-close, nested_count++/nested_count--, etc. It is quite easy to forget
to write the "release" part of the resource management "contract". Sometimes the resource release function is never invoked: this can happen when the control flow leaves the scope because of return or an exception. It is too dangerous to trust the programmer
that he or she will invoke resource release operation in all possible cases in the present and in the future. Some examples are given below.

void foo ()
{
char * ch = new char [100];
if (...)
if (...)
return;
else if (...)
if (...)
else
throw "ERROR"; delete [] ch; // This may not be invoked... memory leak!
}
void bar ()
{
lock.acquire();
if (...)
if (...)
return;
else
throw "ERROR"; lock.release(); // This may not be invoked... deadlock!
}

This is in general control flow abstraction problem. Resource Acquisition is Initialization (RAII) is an extremely popular idiom in C++
that relieves the burden of calling "resource release" operation in a clever way.

Solution and Sample Code

The idea is to wrap the resource release operation in a destructor of an object in the scope. Language guarantees that the destructor will
always be invoked (of a successfully constructed object) when control flow leaves the scope because of a return statement or an exception.

//  Private copy constructor and copy assignment ensure classes derived
// from class NonCopyable cannot be copied.
class NonCopyable
{
NonCopyable (NonCopyable const &); // private copy constructor
NonCopyable & operator = (NonCopyable const &); // private assignment operator
};
template <class T>
class AutoDelete : NonCopyable
{
public:
AutoDelete (T * p = 0) : ptr_(p) {}
~AutoDelete () throw() { delete ptr_; }
private:
T *ptr_;
}; class ScopedLock : NonCopyable// Scoped Lock idiom
{
public:
ScopedLock (Lock & l) : lock_(l) { lock_.acquire(); }
~ScopedLock () throw () { lock_.release(); }
private:
Lock& lock_;
}; void foo ()
{
X * p = new X;
AutoDelete<X> safe_del(p); // Memory will not leak
p = 0;
// Although, above assignment "p = 0" is not necessary
// as we would not have a dangling pointer in this example.
// It is a good programming practice. if (...)
if (...)
return; // No need to call delete here.
// Destructor of safe_del will delete memory
}
void X::bar()
{
ScopedLock safe_lock(l); // Lock will be released certainly
if (...)
if (...)
throw "ERROR";
// No need to call release here.
// Destructor of safe_lock will release the lock
}

Acquiring resource(s) in constructor is not mandatory in RAII idiom but releasing resources in the destructor is the key. Therefore, it is also known (rarely though) as Resource Release is Finalization idiom.
It is important in this idiom that the destructor should not throw exceptions. Therefore, the destructors have no-throw specification but it is optional. std::auto_ptr and boost::scoped_ptr are ways of quickly using RAII idiom for memory resources. RAII is
also used to ensure exception safety. RAII makes it possible to avoid resource leaks without extensive use of try/catch blocks and is widely used in the software industry.

Many classes that manage resources using RAII, do not have legitimate copy semantics (e.g., network connections, database cursors, mutex). The NonCopyable class shown before prevents copying of objects that
implement RAII. It simply prevents access to the copy-constructor and the copy-assignment operator by making them private. boost::scoped_ptr is an example of one such class that prevents copying while holding memory resources. The NonCopyable class
states this intention explicitly and prevents compilation if used incorrectly. Such classes should not be used in STL containers. However, every resource management class that implements RAII does not have to be non-copyable like the above two examples. If
copy semantics are desired, boost::shared_ptr can be used to manage memory resources. In general, non-intrusivereference counting is used to provide copy semantics as well as RAII.

Consequences

RAII is not without its limitations. The resources which are not memory and must be released deterministically and may
throw exceptions usually aren't handled very well by C++ destructors. That's because a C++ destructor can't propagate errors to the enclosing scope (which is potentially winding up). It has no return value and it should not propagate exceptions outside itself.
If exceptions are possible, then the destructor must handle the exceptional case within itself somehow. Nevertheless, RAII remains the most widely used resource management idiom in C++.


Known Uses

  • Virtually all non-trivial C++ software
  • std::auto_ptr
  • boost::scoped_ptr
  • boost::mutex::scoped_lock

Related Idioms

Scope Guard

Reference Counting

Non copyable
Scoped
Locking
 idiom is a special case of RAII applied to operating system synchronization primitives such as mutex and semaphores

Reference






Resource Acquisition Is Initialization(RAII Idiom)的更多相关文章

  1. Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization

    w https://zh.wikipedia.org/wiki/RAII RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的 ...

  2. RAII(Resource Acquisition Is Initialization)简介

    RAII(Resource Acquisition Is Initialization),也称为“资源获取就是初始化”,是C++语言的一种管理资源.避免泄漏的惯用法.C++标准保证任何情况下,已构造的 ...

  3. RAII(Resource Acquisition Is Initialization)资源获得式初始化

    当在编写代码中用到异常,非常重要的一点是:“如果异常发生,程序占用的资源都被正确地清理了吗?” 大多数情况下不用担心,但是在构造函数里有一个特殊的问题:如果一个对象的构造函数在执行过程中抛出异常,那么 ...

  4. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Initialization of bean failed

  5. <Effective C++>读书摘要--Resource Management<一>

    1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphi ...

  6. C++学习指南

    转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...

  7. The Definitive C++ Book Guide and List

    学习c++的书单 转自 http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Beginner ...

  8. More C++ Idioms

    Table of Contents Note: synonyms for each idiom are listed in parentheses. Adapter Template TODO Add ...

  9. The Definitive C++ Book Guide and List--reference

    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Reference Style - All ...

随机推荐

  1. 通过实战理解C语言精要——函数篇

      前言 本篇博客是对C语言函数部分的重点内容和细枝末节通过实战得到的经验的总结精炼,不涵盖C语言函数的全部内容,所有提炼内容均来自提炼与实战,阅读需要对函数部分有一定基础,可用于对C语言函数的理解提 ...

  2. SQLite剖析之动态内存分配

    SQLite通过动态内存分配来获取各种对象(例如数据库连接和SQL预处理语句)所需内存.建立数据库文件的内存Cache.保存查询结果. 1.特性    SQLite内核和它的内存分配子系统提供以下特性 ...

  3. RSVP协议的基本概念介绍

    2010-06-12 14:12 佚名 互联网 字号:T | T 对于RSVP协议的简单介绍和图解.通过文章,我们将对这个含义的基本概念和结构,以及工作方式等方面的知识有所了解.希望对大家有所帮助. ...

  4. sendEmail

    原文:http://blog.chinaunix.net/uid-16844903-id-308853.html 功能: 发邮件的客户端 官网地址: http://caspian.dotconf.ne ...

  5. maven环境配置+eclipse环境配置

    一 . (1),下载maven 有点类似于tomcat 解压后就可以用 ,不用安装 (2), 配置环境变量 在系统变量的path 中添加  E:\01Server\maven\bin    注意是bi ...

  6. onscroll事件,onresize事件

    js获取页面元素高度.宽度 网页可见区域宽: document.body.clientWidth;  网页可见区域高: document.body.clientHeight;  网页可见区域宽: do ...

  7. [Unity3D]脚本生命周期学习

    脚本的生命周期 继承于MonoBehaviour的类对象无需手动实例化,由引擎来决定实例化的时机 Awake:每当脚本被加载的时候调用一次,就是说即使脚本没有被勾选,也会调用一次,主要用来做一些初始化 ...

  8. Android接入支付宝和银联

    支付宝接入参考链接:https://software.intel.com/zh-cn/node/542608 银联接入参考链接:http://blog.csdn.net/gf771115/articl ...

  9. TransactionScope 之分布式配置

    .Net开发过程中,涉及多个数据库和不同数据库的分布式事务(Distributed Transaction)开发,有时会碰到“与基础事务管理器的通信失败”的错误.导致这个错误一般有下列三个原因:1)  ...

  10. Unity里的Mesh属性

    ----------------------------------------------------------------------------------------------- Mesh ...