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 DAdaVala, 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 lifetimeresource 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的更多相关文章

  1. Resource Acquisition Is Initialization(RAII Idiom)

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

  2. RAII(Resource Acquisition Is Initialization)简介

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

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

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

  4. __attribute__中constructor和destructor

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  5. Constructor and destructor -- Initialization & Cleanup in C++

    Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...

  6. __attribute__中constructor和destructor[总结]

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  7. 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

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

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

  9. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

随机推荐

  1. 一个简单的MariaDB认证插件demo

    代码地址如下:http://www.demodashi.com/demo/13076.html 一.前言 众所周知(其实可能很多人不知道)MariaDB支持插件认证.在MariaDB中新建用户,常见的 ...

  2. SVN学习(二)——SVN 提交、更新、解决冲突等操作步骤

    1. 纳入版本控制 ①新建文件abc.txt ②在文件上点右键 ③添加后文件图标发生变化 2. 提交 ①使用TortoiseSVN可以提交具体某一个文件,或某一个目录下的所有改变.方法就是在想要提交的 ...

  3. redis bgsave 内存不够

    客户反应很慢,程序玩不了,登录服务器,查看,发现cpu/io正常,内存也还有,但是负载很离谱的飙高到了30多,查原因: 因为用了redis,最近redis数据增长量很大,而且优化过,怀疑是redis问 ...

  4. mysql调用存储过程出现Illegal mix of collations错误

    执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...

  5. L​i​n​u​x​关​闭​防​火​墙​命​令

    (1) 重启后永久性生效: 开启: chkconfig iptables on 关闭: chkconfig iptables off (2) 即时生效,重启后失效: 开启: service iptab ...

  6. jq时间戳转化为可视化时间

    //2016年5月21日 23:12:07 function getDateTimeToDate(dt){ var dateTime = new Date(dt); var date = dateTi ...

  7. Restore IP Addresses -- LeetCode

    原题链接: http://oj.leetcode.com/problems/restore-ip-addresses/  这道题的解法很接近于NP问题.也是採用递归的解法. 基本思路就是取出一个合法的 ...

  8. AndroidManifest.xml配置

    AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅.下面是一个标准的AndroidManifest.xm ...

  9. [精]Oracle 11G数据库VMware虚拟机下载

    虚拟机文件下载地址: http://pan.baidu.com/s/1pJL5Tub 备份一个防止失效 https://pan.baidu.com/s/1nvbZQad Readme文件: 数据库版本 ...

  10. iOSXib布局后代码修改约束的值

      如何修改autolayout 约束的值? 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFormat 语言 4. ...