<s>
Resource	B-Application
acquisition	I-Application
is	I-Application
initialization	I-Application
(	O
RAII	B-Application
)	O
is	O
a	O
programming	O
idiom	O
used	O
in	O
several	O
object-oriented	B-Language
,	O
statically-typed	O
programming	O
languages	O
to	O
describe	O
a	O
particular	O
language	O
behavior	O
.	O
</s>
<s>
In	O
RAII	B-Application
,	O
holding	O
a	O
resource	O
is	O
a	O
class	B-Application
invariant	I-Application
,	O
and	O
is	O
tied	O
to	O
object	O
lifetime	O
.	O
</s>
<s>
Resource	B-Algorithm
allocation	I-Algorithm
(	O
or	O
acquisition	O
)	O
is	O
done	O
during	O
object	O
creation	O
(	O
specifically	O
initialization	O
)	O
,	O
by	O
the	O
constructor	O
,	O
while	O
resource	O
deallocation	O
(	O
release	O
)	O
is	O
done	O
during	O
object	O
destruction	O
(	O
specifically	O
finalization	O
)	O
,	O
by	O
the	O
destructor	B-Language
.	O
</s>
<s>
Thus	O
the	O
resource	O
is	O
guaranteed	O
to	O
be	O
held	O
between	O
when	O
initialization	O
finishes	O
and	O
finalization	O
starts	O
(	O
holding	O
the	O
resources	O
is	O
a	O
class	B-Application
invariant	I-Application
)	O
,	O
and	O
to	O
be	O
held	O
only	O
when	O
the	O
object	O
is	O
alive	O
.	O
</s>
<s>
RAII	B-Application
is	O
associated	O
most	O
prominently	O
with	O
C++	B-Language
where	O
it	O
originated	O
,	O
but	O
also	O
D	B-Application
,	O
Ada	B-Language
,	O
Vala	B-Language
,	O
and	O
Rust	B-Application
.	O
</s>
<s>
The	O
technique	O
was	O
developed	O
for	O
exception-safe	O
resource	O
management	O
in	O
C++	B-Language
during	O
1984	O
–	O
89	O
,	O
primarily	O
by	O
Bjarne	O
Stroustrup	O
and	O
Andrew	O
Koenig	O
,	O
and	O
the	O
term	O
itself	O
was	O
coined	O
by	O
Stroustrup	O
.	O
</s>
<s>
RAII	B-Application
is	O
generally	O
pronounced	O
as	O
an	O
initialism	O
,	O
sometimes	O
pronounced	O
as	O
"	O
R	O
,	O
A	O
,	O
double	O
I	O
"	O
.	O
</s>
<s>
Other	O
names	O
for	O
this	O
idiom	O
include	O
Constructor	O
Acquires	O
,	O
Destructor	B-Language
Releases	O
(	O
CADRe	O
)	O
and	O
one	O
particular	O
style	O
of	O
use	O
is	O
called	O
Scope-based	B-Application
Resource	I-Application
Management	I-Application
(	O
SBRM	B-Application
)	O
.	O
</s>
<s>
This	O
latter	O
term	O
is	O
for	O
the	O
special	O
case	O
of	O
automatic	B-General_Concept
variables	I-General_Concept
.	O
</s>
<s>
RAII	B-Application
ties	O
resources	O
to	O
object	O
lifetime	O
,	O
which	O
may	O
not	O
coincide	O
with	O
entry	O
and	O
exit	O
of	O
a	O
scope	B-Language
.	O
</s>
<s>
(	O
Notably	O
variables	O
allocated	O
on	O
the	O
free	O
store	O
have	O
lifetimes	O
unrelated	O
to	O
any	O
given	O
scope	B-Language
.	O
)	O
</s>
<s>
However	O
,	O
using	O
RAII	B-Application
for	O
automatic	B-General_Concept
variables	I-General_Concept
(	O
SBRM	B-Application
)	O
is	O
the	O
most	O
common	O
use	O
case	O
.	O
</s>
<s>
The	O
following	O
C++11	B-Language
example	O
demonstrates	O
usage	O
of	O
RAII	B-Application
for	O
file	O
access	O
and	O
mutex	B-Operating_System
locking	B-Operating_System
:	O
</s>
<s>
This	O
code	O
is	O
exception-safe	O
because	O
C++	B-Language
guarantees	O
that	O
all	O
stack	O
objects	O
are	O
destroyed	O
at	O
the	O
end	O
of	O
the	O
enclosing	O
scope	B-Language
,	O
known	O
as	O
stack	O
unwinding	O
.	O
</s>
<s>
The	O
destructors	B-Language
of	O
both	O
the	O
lock	O
and	O
file	O
objects	O
are	O
therefore	O
guaranteed	O
to	O
be	O
called	O
when	O
returning	O
from	O
the	O
function	O
,	O
whether	O
an	O
exception	B-General_Concept
has	O
been	O
thrown	O
or	O
not	O
.	O
</s>
<s>
Local	O
variables	O
allow	O
easy	O
management	O
of	O
multiple	O
resources	O
within	O
a	O
single	O
function	O
:	O
they	O
are	O
destroyed	O
in	O
the	O
reverse	O
order	O
of	O
their	O
construction	O
,	O
and	O
an	O
object	O
is	O
destroyed	O
only	O
if	O
fully	O
constructedthat	O
is	O
,	O
if	O
no	O
exception	B-General_Concept
propagates	O
from	O
its	O
constructor	O
.	O
</s>
<s>
Using	O
RAII	B-Application
greatly	O
simplifies	O
resource	O
management	O
,	O
reduces	O
overall	O
code	O
size	O
and	O
helps	O
ensure	O
program	O
correctness	O
.	O
</s>
<s>
RAII	B-Application
is	O
therefore	O
recommended	O
by	O
industry-standard	O
guidelines	O
,	O
</s>
<s>
and	O
most	O
of	O
the	O
C++	B-Language
standard	O
library	O
follows	O
the	O
idiom	O
.	O
</s>
<s>
The	O
advantages	O
of	O
RAII	B-Application
as	O
a	O
resource	O
management	O
technique	O
are	O
that	O
it	O
provides	O
encapsulation	O
,	O
exception	B-General_Concept
safety	O
(	O
for	O
stack	O
resources	O
)	O
,	O
and	O
locality	O
(	O
it	O
allows	O
acquisition	O
and	O
release	O
logic	O
to	O
be	O
written	O
next	O
to	O
each	O
other	O
)	O
.	O
</s>
<s>
Exception	B-General_Concept
safety	O
is	O
provided	O
for	O
stack	O
resources	O
(	O
resources	O
that	O
are	O
released	O
in	O
the	O
same	O
scope	B-Language
as	O
they	O
are	O
acquired	O
)	O
by	O
tying	O
the	O
resource	O
to	O
the	O
lifetime	O
of	O
a	O
stack	O
variable	O
(	O
a	O
local	O
variable	O
declared	O
in	O
a	O
given	O
scope	B-Language
)	O
:	O
if	O
an	O
exception	B-General_Concept
is	O
thrown	O
,	O
and	O
proper	O
exception	B-General_Concept
handling	I-General_Concept
is	O
in	O
place	O
,	O
the	O
only	O
code	O
that	O
will	O
be	O
executed	O
when	O
exiting	O
the	O
current	O
scope	B-Language
are	O
the	O
destructors	B-Language
of	O
objects	O
declared	O
in	O
that	O
scope	B-Language
.	O
</s>
<s>
Finally	O
,	O
locality	O
of	O
definition	O
is	O
provided	O
by	O
writing	O
the	O
constructor	O
and	O
destructor	B-Language
definitions	O
next	O
to	O
each	O
other	O
in	O
the	O
class	O
definition	O
.	O
</s>
<s>
The	O
RAII	B-Application
design	O
is	O
often	O
used	O
for	O
controlling	O
mutex	B-Operating_System
locks	O
in	O
multi-threaded	O
applications	O
.	O
</s>
<s>
Without	O
RAII	B-Application
in	O
this	O
scenario	O
the	O
potential	O
for	O
deadlock	B-Operating_System
would	O
be	O
high	O
and	O
the	O
logic	O
to	O
lock	O
the	O
mutex	B-Operating_System
would	O
be	O
far	O
from	O
the	O
logic	O
to	O
unlock	O
it	O
.	O
</s>
<s>
With	O
RAII	B-Application
,	O
the	O
code	O
that	O
locks	O
the	O
mutex	B-Operating_System
essentially	O
includes	O
the	O
logic	O
that	O
the	O
lock	O
will	O
be	O
released	O
when	O
execution	O
leaves	O
the	O
scope	B-Language
of	O
the	O
RAII	B-Application
object	O
.	O
</s>
<s>
Another	O
typical	O
example	O
is	O
interacting	O
with	O
files	O
:	O
We	O
could	O
have	O
an	O
object	O
that	O
represents	O
a	O
file	O
that	O
is	O
open	O
for	O
writing	O
,	O
wherein	O
the	O
file	O
is	O
opened	O
in	O
the	O
constructor	O
and	O
closed	O
when	O
execution	O
leaves	O
the	O
object	O
's	O
scope	B-Language
.	O
</s>
<s>
In	O
both	O
cases	O
,	O
RAII	B-Application
ensures	O
only	O
that	O
the	O
resource	O
in	O
question	O
is	O
released	O
appropriately	O
;	O
care	O
must	O
still	O
be	O
taken	O
to	O
maintain	O
exception	B-General_Concept
safety	O
.	O
</s>
<s>
If	O
the	O
code	O
modifying	O
the	O
data	O
structure	O
or	O
file	O
is	O
not	O
exception-safe	O
,	O
the	O
mutex	B-Operating_System
could	O
be	O
unlocked	O
or	O
the	O
file	O
closed	O
with	O
the	O
data	O
structure	O
or	O
file	O
corrupted	O
.	O
</s>
<s>
Ownership	O
of	O
dynamically	O
allocated	O
objects	O
(	O
memory	O
allocated	O
with	O
new	O
in	O
C++	B-Language
)	O
can	O
also	O
be	O
controlled	O
with	O
RAII	B-Application
,	O
such	O
that	O
the	O
object	O
is	O
released	O
when	O
the	O
RAII	B-Application
(	O
stack-based	O
)	O
object	O
is	O
destroyed	O
.	O
</s>
<s>
For	O
this	O
purpose	O
,	O
the	O
C++11	B-Language
standard	O
library	O
defines	O
the	O
smart	B-Language
pointer	I-Language
classes	O
std::unique_ptr	O
for	O
single-owned	O
objects	O
and	O
std::shared_ptr	O
for	O
objects	O
with	O
shared	O
ownership	O
.	O
</s>
<s>
Similar	O
classes	O
are	O
also	O
available	O
through	O
std::auto_ptr	B-Language
in	O
C++98	O
,	O
and	O
boost::shared_ptr	O
in	O
the	O
Boost	B-Language
libraries	I-Language
.	O
</s>
<s>
Both	O
Clang	B-Application
and	O
the	O
GNU	B-Application
Compiler	I-Application
Collection	I-Application
implement	O
a	O
non-standard	O
extension	O
to	O
the	O
C	B-Language
language	I-Language
to	O
support	O
RAII	B-Application
:	O
the	O
"	O
cleanup	O
"	O
variable	O
attribute	O
.	O
</s>
<s>
The	O
following	O
annotates	O
a	O
variable	O
with	O
a	O
given	O
destructor	B-Language
function	O
that	O
it	O
will	O
call	O
when	O
the	O
variable	O
goes	O
out	O
of	O
scope	B-Language
:	O
</s>
<s>
RAII	B-Application
only	O
works	O
for	O
resources	O
acquired	O
and	O
released	O
(	O
directly	O
or	O
indirectly	O
)	O
by	O
stack-allocated	O
objects	O
,	O
</s>
<s>
Heap-allocated	O
objects	O
which	O
themselves	O
acquire	O
and	O
release	O
resources	O
are	O
common	O
in	O
many	O
languages	O
,	O
including	O
C++	B-Language
.	O
</s>
<s>
RAII	B-Application
depends	O
on	O
heap-based	O
objects	O
to	O
be	O
implicitly	O
or	O
explicitly	O
deleted	O
along	O
all	O
possible	O
execution	O
paths	O
,	O
in	O
order	O
to	O
trigger	O
its	O
resource-releasing	O
destructor	B-Language
(	O
or	O
equivalent	O
)	O
.	O
</s>
<s>
This	O
can	O
be	O
achieved	O
by	O
using	O
smart	B-Language
pointers	I-Language
to	O
manage	O
all	O
heap	O
objects	O
,	O
with	O
weak-pointers	O
for	O
cyclically	O
referenced	O
objects	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
stack	O
unwinding	O
is	O
only	O
guaranteed	O
to	O
occur	O
if	O
the	O
exception	B-General_Concept
is	O
caught	O
somewhere	O
.	O
</s>
<s>
(	O
C++03	O
standard	O
,	O
§	O
15.3/9	O
)	O
.	O
</s>
<s>
Perl	B-Language
,	O
Python	B-Language
(	O
in	O
the	O
CPython	B-Language
implementation	O
)	O
,	O
and	O
PHP	B-Application
manage	O
object	O
lifetime	O
by	O
reference	B-General_Concept
counting	I-General_Concept
,	O
which	O
makes	O
it	O
possible	O
to	O
use	O
RAII	B-Application
.	O
</s>
<s>
Objects	O
that	O
are	O
no	O
longer	O
referenced	O
are	O
immediately	O
destroyed	O
or	O
finalized	O
and	O
released	O
,	O
so	O
a	O
destructor	B-Language
or	O
finalizer	B-Application
can	O
release	O
the	O
resource	O
at	O
that	O
time	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
not	O
always	O
idiomatic	O
in	O
such	O
languages	O
,	O
and	O
is	O
specifically	O
discouraged	O
in	O
Python	B-Language
(	O
in	O
favor	O
of	O
context	O
managers	O
and	O
finalizers	B-Application
from	O
the	O
weakref	O
package	O
)	O
.	O
</s>
<s>
However	O
,	O
object	O
lifetimes	O
are	O
not	O
necessarily	O
bound	O
to	O
any	O
scope	B-Language
,	O
and	O
objects	O
may	O
be	O
destroyed	O
non-deterministically	O
or	O
not	O
at	O
all	O
.	O
</s>
<s>
This	O
makes	O
it	O
possible	O
to	O
accidentally	O
leak	O
resources	O
that	O
should	O
have	O
been	O
released	O
at	O
the	O
end	O
of	O
some	O
scope	B-Language
.	O
</s>
<s>
Objects	O
stored	O
in	O
a	O
static	B-General_Concept
variable	I-General_Concept
(	O
notably	O
a	O
global	O
variable	O
)	O
may	O
not	O
be	O
finalized	O
when	O
the	O
program	O
terminates	O
,	O
so	O
their	O
resources	O
are	O
not	O
released	O
;	O
CPython	B-Language
makes	O
no	O
guarantee	O
of	O
finalizing	O
such	O
objects	O
,	O
for	O
instance	O
.	O
</s>
<s>
In	O
CPython	B-Language
there	O
is	O
a	O
cycle	O
detector	O
which	O
detects	O
cycles	O
and	O
finalizes	O
the	O
objects	O
in	O
the	O
cycle	O
,	O
though	O
prior	O
to	O
CPython	B-Language
3.4	O
,	O
cycles	O
are	O
not	O
collected	O
if	O
any	O
object	O
in	O
the	O
cycle	O
has	O
a	O
finalizer	B-Application
.	O
</s>
