<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
the	O
dispose	B-Application
pattern	I-Application
is	O
a	O
design	O
pattern	O
for	O
resource	B-General_Concept
management	O
.	O
</s>
<s>
In	O
this	O
pattern	O
,	O
a	O
resource	B-General_Concept
is	O
held	O
by	O
an	O
object	O
,	O
and	O
released	O
by	O
calling	O
a	O
conventional	O
method	B-Language
–	O
usually	O
called	O
close	O
,	O
dispose	B-Application
,	O
free	O
,	O
release	O
depending	O
on	O
the	O
language	O
–	O
which	O
releases	O
any	O
resources	O
the	O
object	O
is	O
holding	O
onto	O
.	O
</s>
<s>
Many	O
programming	O
languages	O
offer	O
language	B-Language
constructs	I-Language
to	O
avoid	O
having	O
to	O
call	O
the	O
dispose	B-Application
method	B-Language
explicitly	O
in	O
common	O
situations	O
.	O
</s>
<s>
The	O
dispose	B-Application
pattern	I-Application
is	O
primarily	O
used	O
in	O
languages	O
whose	O
runtime	B-Device
environment	I-Device
have	O
automatic	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
(	O
see	O
motivation	O
below	O
)	O
.	O
</s>
<s>
Wrapping	O
resources	O
in	O
objects	O
is	O
the	O
object-oriented	B-Language
form	O
of	O
encapsulation	B-Application
,	O
and	O
underlies	O
the	O
dispose	B-Application
pattern	I-Application
.	O
</s>
<s>
Resources	O
are	O
typically	O
represented	O
by	O
handles	O
(	O
abstract	O
references	O
)	O
,	O
concretely	O
usually	O
integers	O
,	O
which	O
are	O
used	O
to	O
communicate	O
with	O
an	O
external	O
system	O
that	O
provides	O
the	O
resource	B-General_Concept
.	O
</s>
<s>
For	O
example	O
,	O
files	O
are	O
provided	O
by	O
the	O
operating	B-General_Concept
system	I-General_Concept
(	O
specifically	O
the	O
file	B-Application
system	I-Application
)	O
,	O
which	O
in	O
many	O
systems	O
represents	O
open	O
files	O
with	O
a	O
file	B-Application
descriptor	I-Application
(	O
an	O
integer	O
representing	O
the	O
file	O
)	O
.	O
</s>
<s>
These	O
handles	O
can	O
be	O
used	O
directly	O
,	O
by	O
storing	O
the	O
value	O
in	O
a	O
variable	O
and	O
passing	O
it	O
as	O
an	O
argument	O
to	O
functions	O
that	O
use	O
the	O
resource	B-General_Concept
.	O
</s>
<s>
However	O
,	O
it	O
is	O
frequently	O
useful	O
to	O
abstract	O
from	O
the	O
handle	O
itself	O
(	O
for	O
example	O
,	O
if	O
different	O
operating	B-General_Concept
systems	I-General_Concept
represent	O
files	O
differently	O
)	O
,	O
and	O
to	O
store	O
additional	O
auxiliary	O
data	O
with	O
the	O
handle	O
,	O
so	O
handles	O
can	O
be	O
stored	O
as	O
a	O
field	O
in	O
a	O
record	O
,	O
along	O
with	O
other	O
data	O
;	O
if	O
this	O
in	O
an	O
opaque	B-General_Concept
data	I-General_Concept
type	I-General_Concept
,	O
then	O
this	O
provides	O
information	B-Application
hiding	I-Application
and	O
the	O
user	O
is	O
abstracted	O
from	O
the	O
actual	O
representation	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
C	B-Language
file	I-Language
input/output	I-Language
,	O
files	O
are	O
represented	O
by	O
objects	O
of	O
the	O
FILE	O
type	O
(	O
confusingly	O
called	O
"	O
file	B-Application
handles	I-Application
"	O
:	O
these	O
are	O
a	O
language-level	O
abstraction	O
)	O
,	O
which	O
stores	O
an	O
(	O
operating	B-General_Concept
system	I-General_Concept
)	O
handle	O
to	O
the	O
file	O
(	O
such	O
as	O
a	O
file	B-Application
descriptor	I-Application
)	O
,	O
together	O
with	O
auxiliary	O
information	O
like	O
I/O	O
mode	O
(	O
reading	O
,	O
writing	O
)	O
and	O
position	O
in	O
the	O
stream	O
.	O
</s>
<s>
These	O
objects	O
are	O
created	O
by	O
calling	O
fopen	O
(	O
in	O
object-oriented	B-Language
terms	O
,	O
a	O
constructor	O
)	O
,	O
which	O
acquires	O
the	O
resource	B-General_Concept
and	O
returns	O
a	O
pointer	O
to	O
it	O
;	O
the	O
resource	B-General_Concept
is	O
released	O
by	O
calling	O
fclose	O
on	O
a	O
pointer	O
to	O
the	O
FILE	O
object	O
.	O
</s>
<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
this	O
is	O
instead	O
an	O
instance	B-Language
method	I-Language
on	O
a	O
file	O
object	O
,	O
as	O
in	O
Python	B-Language
:	O
</s>
<s>
This	O
is	O
precisely	O
the	O
dispose	B-Application
pattern	I-Application
,	O
and	O
only	O
differs	O
in	O
syntax	O
and	O
code	O
structure	O
from	O
traditional	O
file	O
opening	O
and	O
closing	O
.	O
</s>
<s>
Other	O
resources	O
can	O
be	O
managed	O
in	O
exactly	O
the	O
same	O
way	O
:	O
being	O
acquired	O
in	O
a	O
constructor	O
or	O
factory	O
,	O
and	O
released	O
by	O
an	O
explicit	O
close	O
or	O
dispose	B-Application
method	B-Language
.	O
</s>
<s>
If	O
a	O
resource	B-General_Concept
is	O
unlimited	O
or	O
effectively	O
unlimited	O
,	O
and	O
no	O
explicit	O
finalization	O
is	O
necessary	O
,	O
it	O
is	O
not	O
important	O
to	O
release	O
it	O
,	O
and	O
in	O
fact	O
short-lived	O
programs	O
often	O
do	O
not	O
explicitly	O
release	O
resources	O
:	O
due	O
to	O
short	O
run	O
time	O
,	O
they	O
are	O
unlikely	O
to	O
exhaust	O
resources	O
,	O
and	O
they	O
rely	O
on	O
the	O
runtime	B-Device
system	I-Device
or	O
operating	B-General_Concept
system	I-General_Concept
to	O
do	O
any	O
finalization	O
.	O
</s>
<s>
Explicit	O
disposal	O
means	O
that	O
resource	B-General_Concept
finalization	O
and	O
release	O
is	O
deterministic	O
and	O
prompt	O
:	O
the	O
dispose	B-Application
method	B-Language
does	O
not	O
complete	O
until	O
these	O
are	O
done	O
.	O
</s>
<s>
An	O
alternative	O
to	O
requiring	O
explicit	O
disposal	O
is	O
to	O
tie	O
resource	B-General_Concept
management	O
to	O
object	O
lifetime	O
:	O
resources	O
are	O
acquired	O
during	O
object	O
creation	O
,	O
and	O
released	O
during	O
object	O
destruction	O
.	O
</s>
<s>
This	O
approach	O
is	O
known	O
as	O
the	O
Resource	B-Application
Acquisition	I-Application
Is	I-Application
Initialization	I-Application
(	O
RAII	B-Application
)	O
idiom	O
,	O
and	O
is	O
used	O
in	O
languages	O
with	O
deterministic	O
memory	O
management	O
(	O
e.g.	O
</s>
<s>
C++	B-Language
)	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
in	O
the	O
example	O
above	O
,	O
the	O
resource	B-General_Concept
is	O
acquired	O
when	O
the	O
file	O
object	O
is	O
created	O
,	O
and	O
when	O
the	O
scope	O
of	O
the	O
variable	O
f	O
is	O
exited	O
,	O
the	O
file	O
object	O
that	O
f	O
refers	O
to	O
is	O
destroyed	O
,	O
and	O
as	O
part	O
of	O
this	O
,	O
the	O
resource	B-General_Concept
is	O
released	O
.	O
</s>
<s>
RAII	B-Application
relies	O
on	O
object	O
lifetime	O
being	O
deterministic	O
;	O
however	O
,	O
with	O
automatic	B-General_Concept
memory	I-General_Concept
management	I-General_Concept
,	O
object	O
lifetime	O
is	O
not	O
a	O
concern	O
of	O
the	O
programmer	O
:	O
objects	O
are	O
destroyed	O
at	O
some	O
point	O
after	O
they	O
are	O
no	O
longer	O
used	O
,	O
but	O
when	O
is	O
abstracted	O
.	O
</s>
<s>
Indeed	O
,	O
lifetime	O
is	O
often	O
not	O
deterministic	O
,	O
though	O
it	O
may	O
be	O
,	O
notably	O
if	O
reference	B-General_Concept
counting	I-General_Concept
is	O
used	O
.	O
</s>
<s>
Indeed	O
,	O
in	O
some	O
cases	O
there	O
is	O
no	O
guarantee	O
that	O
objects	O
will	O
ever	O
be	O
finalized	O
:	O
when	O
the	O
program	O
terminates	O
,	O
it	O
may	O
not	O
finalize	O
the	O
objects	O
,	O
and	O
instead	O
just	O
let	O
the	O
operating	B-General_Concept
system	I-General_Concept
reclaim	O
memory	O
;	O
if	O
finalization	O
is	O
required	O
(	O
e.g.	O
,	O
to	O
flush	O
buffers	O
)	O
,	O
data	O
loss	O
can	O
occur	O
.	O
</s>
<s>
Thus	O
by	O
not	O
coupling	O
resource	B-General_Concept
management	O
to	O
object	O
lifetime	O
,	O
the	O
dispose	B-Application
pattern	I-Application
allows	O
resources	O
to	O
be	O
released	O
promptly	O
,	O
while	O
giving	O
implementation	O
flexibility	O
for	O
memory	O
management	O
.	O
</s>
<s>
A	O
key	O
problem	O
with	O
the	O
dispose	B-Application
pattern	I-Application
is	O
that	O
if	O
the	O
dispose	B-Application
method	B-Language
is	O
not	O
called	O
,	O
the	O
resource	B-General_Concept
is	O
leaked	O
.	O
</s>
<s>
If	O
the	O
function	O
returns	O
at	O
the	O
first	O
return	O
,	O
the	O
file	O
is	O
never	O
closed	O
and	O
the	O
resource	B-General_Concept
is	O
leaked	O
.	O
</s>
<s>
If	O
the	O
intervening	O
code	O
raises	O
an	O
exception	O
,	O
the	O
function	O
exits	O
early	O
and	O
the	O
file	O
is	O
never	O
closed	O
,	O
so	O
the	O
resource	B-General_Concept
is	O
leaked	O
.	O
</s>
<s>
This	O
leads	O
to	O
code	O
size	O
bloat	O
,	O
and	O
failure	O
to	O
do	O
so	O
will	O
lead	O
to	O
resource	B-General_Concept
leakage	O
in	O
the	O
program	O
.	O
</s>
<s>
To	O
make	O
the	O
safe	O
use	O
of	O
the	O
dispose	B-Application
pattern	I-Application
less	O
verbose	O
,	O
several	O
languages	O
have	O
some	O
kind	O
of	O
built-in	O
support	O
for	O
resources	O
held	O
and	O
released	O
in	O
the	O
same	O
block	O
of	O
code	O
.	O
</s>
<s>
The	O
C#	B-Application
language	O
features	O
the	O
using	O
statement	O
that	O
automatically	O
calls	O
the	O
Dispose	B-Application
method	B-Language
on	O
an	O
object	O
that	O
implements	O
the	O
IDisposable	O
interface	B-Application
:	O
</s>
<s>
Similarly	O
,	O
the	O
Python	B-Language
language	I-Language
has	O
a	O
with	O
statement	O
that	O
can	O
be	O
used	O
to	O
similar	O
effect	O
with	O
a	O
context	O
manager	O
object	O
.	O
</s>
<s>
The	O
Java	B-Language
language	I-Language
introduced	O
a	O
new	O
syntax	O
called	O
try-with-resources	O
in	O
Java	B-Language
version	O
7	O
.	O
</s>
<s>
It	O
can	O
be	O
used	O
on	O
objects	O
that	O
implement	O
the	O
AutoCloseable	O
interface	B-Application
(	O
that	O
defines	O
method	B-Language
close( )	O
)	O
:	O
</s>
<s>
Beyond	O
the	O
key	O
problem	O
of	O
correct	O
resource	B-General_Concept
management	O
in	O
the	O
presence	O
of	O
returns	O
and	O
exceptions	O
,	O
and	O
heap-based	O
resource	B-General_Concept
management	O
(	O
disposing	O
objects	O
in	O
a	O
different	O
scope	O
from	O
where	O
they	O
are	O
created	O
)	O
,	O
there	O
are	O
many	O
further	O
complexities	O
associated	O
with	O
the	O
dispose	B-Application
pattern	I-Application
.	O
</s>
<s>
These	O
problems	O
are	O
largely	O
avoided	O
by	O
RAII	B-Application
.	O
</s>
<s>
However	O
,	O
in	O
common	O
simple	O
use	O
these	O
complexities	O
do	O
not	O
arise	O
:	O
acquire	O
a	O
single	O
resource	B-General_Concept
,	O
do	O
something	O
with	O
it	O
,	O
automatically	O
release	O
it	O
.	O
</s>
<s>
A	O
fundamental	O
problem	O
is	O
that	O
having	O
a	O
resource	B-General_Concept
is	O
no	O
longer	O
a	O
class	B-Application
invariant	I-Application
(	O
the	O
resource	B-General_Concept
is	O
held	O
from	O
object	O
creation	O
until	O
it	O
is	O
disposed	O
,	O
but	O
the	O
object	O
is	O
still	O
live	O
at	O
this	O
point	O
)	O
,	O
so	O
the	O
resource	B-General_Concept
may	O
not	O
be	O
available	O
when	O
the	O
object	O
tries	O
to	O
use	O
it	O
,	O
for	O
example	O
trying	O
to	O
read	O
from	O
a	O
closed	O
file	O
.	O
</s>
<s>
This	O
means	O
that	O
all	O
methods	O
on	O
the	O
object	O
that	O
use	O
the	O
resource	B-General_Concept
potentially	O
fail	O
,	O
concretely	O
usually	O
by	O
returning	O
an	O
error	O
or	O
raising	O
an	O
exception	O
.	O
</s>
<s>
In	O
practice	O
this	O
is	O
minor	O
,	O
as	O
use	O
of	O
resources	O
can	O
usually	O
fail	O
for	O
other	O
reasons	O
as	O
well	O
(	O
for	O
example	O
,	O
trying	O
to	O
read	O
past	O
the	O
end	O
of	O
a	O
file	O
)	O
,	O
so	O
these	O
methods	O
already	O
might	O
fail	O
,	O
and	O
not	O
having	O
a	O
resource	B-General_Concept
just	O
adds	O
another	O
possible	O
failure	O
.	O
</s>
<s>
A	O
standard	O
way	O
to	O
implement	O
this	O
is	O
to	O
add	O
a	O
boolean	O
field	O
to	O
the	O
object	O
,	O
called	O
disposed	O
,	O
which	O
is	O
set	O
to	O
true	O
by	O
dispose	B-Application
,	O
and	O
checked	O
by	O
a	O
guard	B-Language
clause	I-Language
to	O
all	O
methods	O
(	O
that	O
use	O
the	O
resource	B-General_Concept
)	O
,	O
raising	O
an	O
exception	O
(	O
such	O
as	O
ObjectDisposedException	O
in	O
.NET	B-Application
)	O
if	O
the	O
object	O
has	O
been	O
disposed	O
.	O
</s>
<s>
Further	O
,	O
it	O
is	O
possible	O
to	O
call	O
dispose	B-Application
on	O
an	O
object	O
more	O
than	O
once	O
.	O
</s>
<s>
While	O
this	O
may	O
indicate	O
a	O
programming	O
error	O
(	O
each	O
object	O
holding	O
a	O
resource	B-General_Concept
must	O
be	O
disposed	O
exactly	O
once	O
)	O
,	O
it	O
is	O
simpler	O
,	O
more	O
robust	O
,	O
and	O
thus	O
usually	O
preferable	O
for	O
dispose	B-Application
to	O
be	O
idempotent	O
(	O
meaning	O
"	O
calling	O
multiple	O
times	O
is	O
the	O
same	O
as	O
calling	O
once	O
"	O
)	O
.	O
</s>
<s>
This	O
is	O
easily	O
implemented	O
by	O
using	O
the	O
same	O
boolean	O
disposed	O
field	O
and	O
checking	O
it	O
in	O
a	O
guard	B-Language
clause	I-Language
at	O
the	O
start	O
of	O
dispose	B-Application
,	O
in	O
that	O
case	O
returning	O
immediately	O
,	O
rather	O
than	O
raising	O
an	O
exception	O
.	O
</s>
<s>
Java	B-Language
distinguishes	O
disposable	O
types	O
(	O
those	O
that	O
implement	O
)	O
from	O
disposable	O
types	O
where	O
dispose	B-Application
is	O
idempotent	O
(	O
the	O
subtype	O
)	O
.	O
</s>
<s>
Further	O
,	O
since	O
the	O
dispose	B-Application
pattern	I-Application
usually	O
does	O
not	O
have	O
language	O
support	O
for	O
this	O
,	O
boilerplate	O
code	O
is	O
necessary	O
.	O
</s>
<s>
Firstly	O
,	O
if	O
a	O
derived	O
class	O
overrides	O
a	O
dispose	B-Application
method	B-Language
in	O
the	O
base	O
class	O
,	O
the	O
overriding	O
method	B-Language
in	O
the	O
derived	O
class	O
generally	O
needs	O
to	O
call	O
the	O
dispose	B-Application
method	B-Language
in	O
the	O
base	O
class	O
,	O
in	O
order	O
to	O
properly	O
release	O
resources	O
held	O
in	O
the	O
base	O
.	O
</s>
<s>
Secondly	O
,	O
if	O
an	O
object	O
has	O
a	O
"	O
has	O
a	O
"	O
relationship	O
with	O
another	O
object	O
that	O
holds	O
a	O
resource	B-General_Concept
(	O
i.e.	O
,	O
if	O
an	O
object	O
indirectly	O
uses	O
a	O
resource	B-General_Concept
through	O
another	O
object	O
that	O
directly	O
uses	O
a	O
resource	B-General_Concept
)	O
,	O
should	O
the	O
indirectly	O
using	O
object	O
be	O
disposable	O
?	O
</s>
<s>
This	O
corresponds	O
to	O
whether	O
the	O
relationship	O
is	O
owning	O
(	O
object	B-Language
composition	I-Language
)	O
or	O
viewing	O
(	O
object	B-Language
aggregation	I-Language
)	O
,	O
or	O
even	O
just	O
communicating	O
(	O
association	B-Application
)	O
,	O
and	O
both	O
conventions	O
are	O
found	O
(	O
indirect	O
user	O
is	O
responsible	O
for	O
the	O
resource	B-General_Concept
or	O
is	O
not	O
responsible	O
)	O
.	O
</s>
<s>
If	O
the	O
indirect	O
use	O
is	O
responsible	O
for	O
the	O
resource	B-General_Concept
,	O
it	O
must	O
be	O
disposable	O
,	O
and	O
dispose	B-Application
the	O
owned	O
objects	O
when	O
it	O
is	O
disposed	O
(	O
analogous	O
to	O
destroying	O
or	O
finalizing	O
owned	O
objects	O
)	O
.	O
</s>
<s>
Composition	O
(	O
owning	O
)	O
provides	O
encapsulation	B-Application
(	O
only	O
the	O
object	O
that	O
is	O
used	O
needs	O
to	O
be	O
tracked	O
)	O
,	O
but	O
at	O
the	O
cost	O
of	O
considerable	O
complexity	O
when	O
there	O
are	O
further	O
relationships	O
between	O
objects	O
,	O
while	O
aggregation	O
(	O
viewing	O
)	O
is	O
considerably	O
simpler	O
,	O
at	O
the	O
cost	O
of	O
lacking	O
encapsulation	B-Application
.	O
</s>
<s>
In	O
.NET	B-Application
,	O
convention	O
is	O
to	O
only	O
have	O
direct	O
user	O
of	O
resources	O
be	O
responsible	O
:	O
"	O
You	O
should	O
implement	O
IDisposable	O
only	O
if	O
your	O
type	O
uses	O
unmanaged	O
resources	O
directly.	O
"	O
</s>
<s>
See	O
resource	B-General_Concept
management	O
for	O
details	O
,	O
and	O
further	O
examples	O
.	O
</s>
