Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization
w
https://zh.wikipedia.org/wiki/RAII
RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的释放。在这种要求下,只要对象能正确地析构,就不会出现资源泄露问题。
https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization
Resource acquisition is initialization (RAII)[1] is a programming idiom[2] used in several object-oriented languages, most prominently C++, where it originated, but also D, Ada, Vala, and Rust. The technique was developed for exception-safe resource management in C++[3] during 1984–89, primarily by Bjarne Stroustrup and Andrew Koenig,[4] and the term itself was coined by Stroustrup.[5] RAII is generally pronounced as an initialism, sometimes pronounced as "R, A, double I".[6]
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. 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.
Other names for this idiom include Constructor Acquires, Destructor Releases (CADRe) [7] and one particular style of use is called Scope-based Resource Management (SBRM).[8] This latter term is for the special case of automatic variables. RAII ties resources to object lifetime, which may not coincide with entry and exit of a scope. (Notably variables allocated on the free store have lifetimes unrelated to any given scope.) However, using RAII for automatic variables (SBRM) is the most common use case.
Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization的更多相关文章
- Resource Acquisition Is Initialization(RAII Idiom)
原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...
- RAII(Resource Acquisition Is Initialization)简介
RAII(Resource Acquisition Is Initialization),也称为“资源获取就是初始化”,是C++语言的一种管理资源.避免泄漏的惯用法.C++标准保证任何情况下,已构造的 ...
- RAII(Resource Acquisition Is Initialization)资源获得式初始化
当在编写代码中用到异常,非常重要的一点是:“如果异常发生,程序占用的资源都被正确地清理了吗?” 大多数情况下不用担心,但是在构造函数里有一个特殊的问题:如果一个对象的构造函数在执行过程中抛出异常,那么 ...
- __attribute__中constructor和destructor
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- Constructor and destructor -- Initialization & Cleanup in C++
Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...
- __attribute__中constructor和destructor[总结]
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- 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
- <Effective C++>读书摘要--Resource Management<一>
1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphi ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
随机推荐
- 导入解析excel小结
导入解析excel小结 控制器例子:
- jetty.xml解析
我们知道jetty有一种启动方式是在jetty的根目录中运行命令行:java -jar start.jar,这个命令会调用apache的XmlConfiguration工具类作为启动类,这个类会默认读 ...
- 牛散NO.3:MACD放之四海 假作真时真亦假
大宗商品日线“异曲同工夺命勾魂枪” 话说有实战意义的技术在任何资本市场里都能产生出神奇的效果.不能说放之四海皆准,但至少起到触类旁通的“牵强”吧.大宗商品特别是在国际市场交易的大宗 商品由于是来自各方 ...
- [原创]Linux实现服务延迟启动
在开机启动服务的时候,服务之间有依赖关系,必须在某个服务完全启动后才能启动其他的服务.在这种情况下,就需要用到服务延迟启动的功能.在开机时,延迟几分钟再启动.具体步骤如下:1.写一个实现延时启动的脚本 ...
- 利用Python对文件进行批量重命名——以图片文件为例
效果如下:0001号用户的第 i 张图片 代码: import os class ImageRename(): def __init__(self): self.path = 'C:/Users/lb ...
- Django的自定义标签
Django提供了自定义标签功能,可以方便常用方法的重复使用. 标签的本质就是函数,标签名就是函数名. 注意点: 1.需要到django.template对象. 2.register = templa ...
- 在Linux命令行下发送html格式的邮件
在Linux利用formail+sendmail来发送带图片的邮件 formail接收html格式的文件作为邮件的内容,这样就可以解决发送带图片邮件的问题了,因为html中可以插入图片,只要给出的im ...
- java - day08 - ArrayFounderTrans
package day07_addition; import java.util.Arrays; import java.util.Random; //数组 伸缩.增删.位移.复制 public cl ...
- Boolean 转 string
boolean b=false; String b= String.valueOf(b);
- Window安装Anaconda后,conda不是内部或者外部命令
今天在安装Theano的时候,需要看一下,anaconda已经安装了哪些包.使用命令如下,在控制台,cmd回车输入即可: conda list 但是,显示出错,“conda不是内部或者外部命令”,第一 ...