什么是资源获取初始化(RAII)?


当前回答

Manual memory management is a nightmare that programmers have been inventing ways to avoid since the invention of the compiler. Programming languages with garbage collectors make life easier, but at the cost of performance. In this article - Eliminating the Garbage Collector: The RAII Way, Toptal engineer Peter Goodspeed-Niklaus gives us a peek into the history of garbage collectors and explains how notions of ownership and borrowing can help eliminate garbage collectors without compromising their safety guarantees.

其他回答

这是一个编程习语,简单地说就是您

将资源封装到类中(类的构造函数通常——但不一定是**——获取资源,其析构函数总是释放它) 通过类的本地实例*使用资源 当对象超出作用域时,资源将自动释放

这保证了在使用资源时无论发生什么,它最终都将被释放(无论是由于正常返回、销毁包含的对象,还是抛出异常)。

这是c++中广泛使用的良好实践,因为除了是处理资源的安全方式外,它还使您的代码更加干净,因为您不需要将错误处理代码与主要功能混合在一起。

*更新:“local”可以是一个局部变量,也可以是一个类的非静态成员变量。在后一种情况下,成员变量被初始化并与其所有者对象一起销毁。

** Update2:正如@sbi指出的那样,虽然资源通常是在构造函数内部分配的,但也可以在构造函数外部分配并作为参数传入。

《c++ Programming with Design Patterns Revealed》一书将RAII描述为:

获取所有资源 利用资源 释放资源

在哪里

资源被实现为类,所有指针都有类包装器(使它们成为智能指针)。 通过调用构造函数获取资源,通过调用析构函数隐式释放资源(与获取资源的顺序相反)。

对于一个非常强大的概念来说,这是一个非常糟糕的名字,而且可能是c++开发人员在转向其他语言时最容易忽略的事情之一。有一些人试图将这个概念重命名为作用域绑定资源管理(Scope-Bound Resource Management),尽管它似乎还没有流行起来。

When we say 'Resource' we don't just mean memory - it could be file handles, network sockets, database handles, GDI objects... In short, things that we have a finite supply of and so we need to be able to control their usage. The 'Scope-bound' aspect means that the lifetime of the object is bound to the scope of a variable, so when the variable goes out of scope then the destructor will release the resource. A very useful property of this is that it makes for greater exception-safety. For instance, compare this:

RawResourceHandle* handle=createNewResource();
handle->performInvalidOperation();  // Oops, throws exception
...
deleteResource(handle); // oh dear, never gets called so the resource leaks

用RAII这个

class ManagedResourceHandle {
public:
   ManagedResourceHandle(RawResourceHandle* rawHandle_) : rawHandle(rawHandle_) {};
   ~ManagedResourceHandle() {delete rawHandle; }
   ... // omitted operator*, etc
private:
   RawResourceHandle* rawHandle;
};

ManagedResourceHandle handle(createNewResource());
handle->performInvalidOperation();

在后一种情况下,当抛出异常并展开堆栈时,局部变量将被销毁,这确保了我们的资源被清理并且不会泄漏。

许多人认为RAII是用词不当,但实际上它是这个习语的正确名称,只是没有很好地解释它。

Wikipedia explained behavior in detail: Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically-typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tied to object lifetime: resource allocation (or acquisition) is done during object creation (specifically initialization), by the constructor, while resource deallocation (release) is done during object destruction (specifically finalization), by the destructor. In other words, resource acquisition must succeed for initialization to succeed. Thus the resource is guaranteed to be held between when initialization finishes and finalization starts (holding the resources is a class invariant), and to be held only when the object is alive. Thus if there are no object leaks, there are no resource leaks.

至于名称,它仅仅意味着“资源获取”操作是初始化操作,应该是资源类对象的初始化/构造函数的一部分。换句话说,使用这种习惯用法,使用资源意味着创建一个资源类来保存资源,并在构造类对象时初始化资源。它隐式地建议资源的释放应该在资源类析构函数中对称地发生。

这有什么用呢? 当然,您可以选择不使用这个成语,但是如果您想知道使用这个成语会得到什么,请考虑一下

RAII 对于更大的c++项目来说,在构造函数/析构函数对之外不包含对new或delete(或malloc/free)的单个调用是很常见的。或者实际上根本没有。

你可以避免

Exit: free_resource() //在退出函数前清除资源

或者使用RAII锁,这样你就不会忘记解锁。

RAII概念只是一个C堆栈变量的想法。最简单的解释。