<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
a	O
destructor	B-Language
(	O
sometimes	O
abbreviated	O
dtor	B-Language
)	O
is	O
a	O
method	B-Language
which	O
is	O
invoked	O
mechanically	O
just	O
before	O
the	O
memory	O
of	O
the	O
object	O
is	O
released	O
.	O
</s>
<s>
It	O
can	O
happen	O
when	O
its	O
lifetime	O
is	O
bound	O
to	O
scope	B-Language
and	O
the	O
execution	O
leaves	O
the	O
scope	B-Language
,	O
when	O
it	O
is	O
embedded	O
in	O
another	O
object	O
whose	O
lifetime	O
ends	O
,	O
or	O
when	O
it	O
was	O
allocated	O
dynamically	O
and	O
is	O
released	O
explicitly	O
.	O
</s>
<s>
Its	O
main	O
purpose	O
is	O
to	O
free	O
the	O
resources	B-General_Concept
(	O
memory	O
allocations	O
,	O
open	O
files	O
or	O
sockets	O
,	O
database	B-General_Concept
connections	I-General_Concept
,	O
resource	O
locks	O
,	O
etc	O
.	O
)	O
</s>
<s>
Use	O
of	O
destructors	B-Language
is	O
needed	O
for	O
the	O
process	O
of	O
Resource	B-Application
Acquisition	I-Application
Is	I-Application
Initialization	I-Application
(	O
RAII	B-Application
)	O
.	O
</s>
<s>
With	O
most	O
kinds	O
of	O
automatic	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
algorithms	O
,	O
the	O
releasing	O
of	O
memory	O
may	O
happen	O
a	O
long	O
time	O
after	O
the	O
object	O
becomes	O
unreachable	O
,	O
making	O
destructors	B-Language
(	O
called	O
finalizers	B-Application
in	O
this	O
case	O
)	O
unsuitable	O
for	O
most	O
purposes	O
.	O
</s>
<s>
In	O
such	O
languages	O
,	O
the	O
freeing	O
of	O
resources	B-General_Concept
is	O
done	O
either	O
through	O
a	O
lexical	O
construct	O
(	O
such	O
as	O
try	O
..	O
finally	O
,	O
Python	B-Language
's	O
"	O
with	O
"	O
or	O
Java	O
's	O
"	O
try-with-resources	O
"	O
)	O
,	O
which	O
is	O
the	O
equivalent	O
to	O
RAII	B-Application
,	O
or	O
explicitly	O
by	O
calling	O
a	O
function	O
(	O
equivalent	O
to	O
explicit	O
deletion	O
)	O
;	O
in	O
particular	O
,	O
many	O
object-oriented	B-Language
languages	I-Language
use	O
the	B-Application
Dispose	I-Application
pattern	I-Application
.	O
</s>
<s>
C++	B-Language
:	O
destructors	B-Language
have	O
the	O
same	O
name	O
as	O
the	O
class	O
with	O
which	O
they	O
are	O
associated	O
,	O
but	O
with	O
a	O
tilde	O
(	O
~	O
)	O
prefix	O
.	O
</s>
<s>
D	B-Application
:	O
destructors	B-Language
are	O
declared	O
with	O
name	O
~	O
this( )	O
(	O
whereas	O
constructors	O
are	O
declared	O
with	O
this( )	O
)	O
.	O
</s>
<s>
Object	B-Language
Pascal	I-Language
:	O
destructors	B-Language
have	O
the	O
keyword	O
destructor	B-Language
and	O
can	O
have	O
user-defined	O
names	O
,	O
but	O
are	O
mostly	O
named	O
Destroy	O
.	O
</s>
<s>
Objective-C	B-Language
:	O
the	O
destructor	B-Language
method	B-Language
has	O
the	O
name	O
dealloc	O
.	O
</s>
<s>
Perl	B-Language
:	O
the	O
destructor	B-Language
method	B-Language
has	O
the	O
name	O
DESTROY	O
;	O
in	O
the	O
Moose	B-Language
object	I-Language
system	I-Language
extension	I-Language
,	O
it	O
is	O
named	O
DEMOLISH	O
.	O
</s>
<s>
PHP	B-Application
:	O
In	O
PHP	B-Application
5+	O
,	O
the	O
destructor	B-Language
method	B-Language
has	O
the	O
name	O
__destruct	O
.	O
</s>
<s>
There	O
were	O
no	O
destructors	B-Language
in	O
prior	O
versions	O
of	O
PHP	B-Application
.	O
</s>
<s>
Python	B-Language
:	O
there	O
are	O
__del__	O
methods	O
called	O
destructors	B-Language
by	O
the	O
Python	B-Language
2	O
language	O
guide	O
,	O
but	O
they	O
are	O
actually	O
finalizers	B-Application
as	O
acknowledged	O
in	O
Python	B-Language
3	O
.	O
</s>
<s>
Swift	B-Application
:	O
the	O
destructor	B-Language
method	B-Language
has	O
the	O
name	O
deinit	O
.	O
</s>
<s>
The	O
destructor	B-Language
has	O
the	O
same	O
name	O
as	O
the	O
class	O
,	O
but	O
with	O
a	O
tilde	O
(	O
~	O
)	O
before	O
it	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
class	O
called	O
foo	O
will	O
have	O
the	O
destructor	B-Language
.	O
</s>
<s>
Additionally	O
,	O
destructors	B-Language
have	O
neither	O
parameters	O
nor	O
return	O
types	O
.	O
</s>
<s>
As	O
stated	O
above	O
,	O
a	O
destructor	B-Language
for	O
an	O
object	O
is	O
called	O
whenever	O
the	O
object	O
's	O
lifetime	O
ends	O
.	O
</s>
<s>
If	O
the	O
object	O
was	O
created	O
as	O
an	O
automatic	B-General_Concept
variable	I-General_Concept
,	O
its	O
lifetime	O
ends	O
and	O
the	O
destructor	B-Language
is	O
called	O
automatically	O
when	O
the	O
object	O
goes	O
out	O
of	O
scope	B-Language
.	O
</s>
<s>
Because	O
C++	B-Language
does	O
not	O
have	O
garbage	B-General_Concept
collection	I-General_Concept
,	O
if	O
the	O
object	O
was	O
created	O
with	O
a	O
statement	O
(	O
dynamically	O
on	O
the	O
heap	O
)	O
,	O
then	O
its	O
destructor	B-Language
is	O
called	O
when	O
the	O
operator	O
is	O
applied	O
to	O
a	O
pointer	O
to	O
the	O
object	O
.	O
</s>
<s>
Usually	O
that	O
operation	O
occurs	O
within	O
another	O
destructor	B-Language
,	O
typically	O
the	O
destructor	B-Language
of	O
a	O
smart	B-Language
pointer	I-Language
object	O
.	O
</s>
<s>
In	O
inheritance	O
hierarchies	O
,	O
the	O
declaration	O
of	O
a	O
virtual	O
destructor	B-Language
in	O
the	O
base	O
class	O
ensures	O
that	O
the	O
destructors	B-Language
of	O
derived	O
classes	O
are	O
invoked	O
properly	O
when	O
an	O
object	O
is	O
deleted	O
through	O
a	O
pointer-to-base-class	O
.	O
</s>
<s>
Objects	O
that	O
may	O
be	O
deleted	O
in	O
this	O
way	O
need	O
to	O
inherit	O
a	O
virtual	O
destructor	B-Language
.	O
</s>
<s>
A	O
destructor	B-Language
should	O
never	O
throw	O
an	O
exception	O
.	O
</s>
<s>
This	O
construct	O
makes	O
it	O
possible	O
to	O
write	O
code	O
without	O
having	O
to	O
know	O
if	O
a	O
destructor	B-Language
exists	O
for	O
a	O
given	O
type	O
.	O
</s>
<s>
In	O
older	O
versions	O
of	O
the	O
standard	O
,	O
pseudo-destructors	O
were	O
specified	O
to	O
have	O
no	O
effect	O
,	O
however	O
that	O
was	O
changed	O
in	O
a	O
defect	O
report	O
to	O
make	O
them	O
end	O
the	O
lifetime	O
of	O
the	O
object	O
they	O
are	O
called	O
on	O
.	O
</s>
<s>
A	O
detailed	O
description	O
of	O
this	O
method	B-Language
can	O
be	O
found	O
in	O
Scott	O
Meyers	O
 '	O
popular	O
book	O
,	O
Effective	O
Modern	O
C++	B-Language
(	O
Item	O
11	O
:	O
"	O
Prefer	O
deleted	O
functions	O
to	O
private	O
undefined	O
ones	O
.	O
</s>
<s>
The	O
GNU	B-Application
Compiler	I-Application
Collection	I-Application
's	O
C	B-Language
compiler	O
comes	O
with	O
2	O
extensions	O
that	O
allow	O
implementing	O
destructors	B-Language
:	O
</s>
<s>
The	O
destructor	B-Language
function	O
attribute	O
allows	O
defining	O
global	O
prioritized	O
destructor	B-Language
functions	O
:	O
when	O
main( )	O
returns	O
,	O
these	O
functions	O
are	O
called	O
in	O
priority	O
order	O
before	O
the	O
process	O
terminates	O
.	O
</s>
<s>
The	O
cleanup	O
variable	O
attribute	O
allows	O
attaching	O
a	O
destructor	B-Language
function	O
to	O
a	O
variable	O
:	O
the	O
function	O
is	O
called	O
when	O
the	O
variable	O
goes	O
out	O
of	O
scope	B-Language
.	O
</s>
<s>
Destructors	B-Language
in	O
Xojo	B-Application
(	O
REALbasic	B-Application
)	O
can	O
be	O
in	O
one	O
of	O
two	O
forms	O
.	O
</s>
<s>
Each	O
form	O
uses	O
a	O
regular	O
method	B-Language
declaration	O
with	O
a	O
special	O
name	O
(	O
with	O
no	O
parameters	O
and	O
no	O
return	O
value	O
)	O
.	O
</s>
<s>
The	O
newer	O
form	O
uses	O
the	O
name	O
Destructor	B-Language
.	O
</s>
