<s>
In	O
object-oriented	B-Language
programming	I-Language
languages	I-Language
with	O
garbage	B-General_Concept
collection	I-General_Concept
,	O
object	B-General_Concept
resurrection	I-General_Concept
is	O
when	O
an	O
object	O
comes	O
back	O
to	O
life	O
during	O
the	O
process	O
of	O
object	O
destruction	O
,	O
as	O
a	O
side	O
effect	O
of	O
a	O
finalizer	B-Application
being	O
executed	O
.	O
</s>
<s>
Object	B-General_Concept
resurrection	I-General_Concept
causes	O
a	O
number	O
of	O
problems	O
,	O
particularly	O
that	O
the	O
possibility	O
of	O
object	B-General_Concept
resurrection	I-General_Concept
–	O
even	O
if	O
it	O
does	O
not	O
occur	O
–	O
makes	O
garbage	B-General_Concept
collection	I-General_Concept
significantly	O
more	O
complicated	O
and	O
slower	O
,	O
and	O
is	O
a	O
major	O
reason	O
that	O
finalizers	B-Application
are	O
discouraged	O
.	O
</s>
<s>
Languages	O
deal	O
with	O
object	B-General_Concept
resurrection	I-General_Concept
in	O
various	O
ways	O
.	O
</s>
<s>
In	O
rare	O
circumstances	O
,	O
object	B-General_Concept
resurrection	I-General_Concept
is	O
used	O
to	O
implement	O
certain	O
design	O
patterns	O
,	O
notably	O
an	O
object	O
pool	O
,	O
while	O
in	O
other	O
circumstances	O
resurrection	O
is	O
an	O
undesired	O
bug	O
caused	O
by	O
an	O
error	O
in	O
finalizers	B-Application
,	O
and	O
in	O
general	O
resurrection	O
is	O
discouraged	O
.	O
</s>
<s>
Object	B-General_Concept
resurrection	I-General_Concept
occurs	O
via	O
the	O
following	O
process	O
.	O
</s>
<s>
Then	O
,	O
during	O
object	O
destruction	O
,	O
before	O
the	O
garbage	B-General_Concept
collector	I-General_Concept
deallocates	O
the	O
object	O
,	O
a	O
finalizer	B-Application
method	O
may	O
be	O
run	O
,	O
which	O
may	O
in	O
turn	O
make	O
that	O
object	O
or	O
another	O
garbage	O
object	O
(	O
reachable	O
from	O
the	O
object	O
with	O
a	O
finalizer	B-Application
)	O
reachable	O
again	O
by	O
creating	O
references	O
to	O
it	O
,	O
as	O
a	O
finalizer	B-Application
may	O
contain	O
arbitrary	O
code	O
.	O
</s>
<s>
If	O
this	O
happens	O
,	O
the	O
referenced	O
object	O
–	O
which	O
is	O
not	O
necessarily	O
the	O
finalized	O
object	O
–	O
is	O
no	O
longer	O
garbage	O
,	O
and	O
cannot	O
be	O
deallocated	O
,	O
as	O
otherwise	O
the	O
references	O
to	O
it	O
would	O
become	O
dangling	B-Error_Name
references	I-Error_Name
and	O
cause	O
errors	O
when	O
used	O
,	O
generally	O
program	O
crash	O
or	O
unpredictable	O
behavior	O
.	O
</s>
<s>
Instead	O
,	O
in	O
order	O
to	O
maintain	O
memory	B-Application
safety	I-Application
,	O
the	O
object	O
is	O
returned	O
to	O
life	O
or	O
resurrected	O
.	O
</s>
<s>
In	O
order	O
to	O
detect	O
this	O
,	O
a	O
garbage	B-General_Concept
collector	I-General_Concept
will	O
generally	O
do	O
two-phase	O
collection	O
in	O
the	O
presence	O
of	O
finalizers	B-Application
:	O
first	O
finalize	O
any	O
garbage	O
that	O
has	O
a	O
finalizer	B-Application
,	O
and	O
then	O
re-check	O
all	O
garbage	O
(	O
or	O
all	O
garbage	O
reachable	O
from	O
the	O
objects	O
with	O
finalizers	B-Application
)	O
,	O
in	O
case	O
the	O
finalizers	B-Application
have	O
resurrected	O
some	O
garbage	O
.	O
</s>
<s>
In	O
many	O
languages	O
,	O
notably	O
C#	O
,	O
Java	O
,	O
and	O
Python	O
(	O
from	O
Python	O
3.4	O
)	O
,	O
objects	O
are	O
only	O
finalized	O
once	O
,	O
to	O
avoid	O
the	O
possibility	O
of	O
an	O
object	O
being	O
repeatedly	O
resurrected	O
or	O
even	O
being	O
indestructible	O
;	O
in	O
C#	O
objects	O
with	O
finalizers	B-Application
by	O
default	O
are	O
only	O
finalized	O
once	O
,	O
but	O
can	O
be	O
re-registered	O
for	O
finalization	O
.	O
</s>
<s>
In	O
other	O
cases	O
resurrected	O
objects	O
are	O
considered	O
errors	O
,	O
notably	O
in	O
Objective-C	B-Language
;	O
or	O
treated	O
identically	O
to	O
other	O
objects	O
,	O
notably	O
in	O
Python	O
prior	O
to	O
Python	O
3.4	O
.	O
</s>
<s>
A	O
"	O
zombie	O
object	O
"	O
has	O
a	O
specialized	O
meaning	O
in	O
Objective-C	B-Language
,	O
however	O
,	O
which	O
is	O
detailed	O
below	O
.	O
</s>
<s>
Zombie	O
objects	O
are	O
somewhat	O
analogous	O
to	O
zombie	B-Operating_System
processes	I-Operating_System
,	O
in	O
that	O
they	O
have	O
undergone	O
a	O
termination	O
state	O
change	O
and	O
are	O
close	O
to	O
deallocation	O
,	O
but	O
the	O
details	O
are	O
significantly	O
different	O
.	O
</s>
<s>
In	O
the	O
.NET	B-Application
Framework	I-Application
,	O
notably	O
C#	O
and	O
VB.NET	O
,	O
"	O
object	B-General_Concept
resurrection	I-General_Concept
"	O
instead	O
refers	O
to	O
the	O
state	O
of	O
an	O
object	O
during	O
finalization	O
:	O
the	O
object	O
is	O
brought	O
back	O
to	O
life	O
(	O
from	O
being	O
inaccessible	O
)	O
,	O
the	O
finalizer	B-Application
is	O
run	O
,	O
and	O
then	O
returned	O
to	O
being	O
inaccessible	O
(	O
and	O
no	O
longer	O
is	O
registered	O
for	O
future	O
finalization	O
)	O
.	O
</s>
<s>
In	O
.NET	B-Application
,	O
which	O
objects	O
need	O
finalization	O
is	O
not	O
tracked	O
object-by-object	O
,	O
but	O
instead	O
is	O
stored	O
in	O
a	O
finalization	O
"	O
queue	O
"	O
,	O
so	O
rather	O
than	O
a	O
notion	O
of	O
resurrected	O
objects	O
in	O
the	O
sense	O
of	O
this	O
article	O
,	O
one	O
speaks	O
of	O
objects	O
"	O
queued	O
for	O
finalization	O
"	O
.	O
</s>
<s>
There	O
are	O
two	O
main	O
ways	O
that	O
an	O
object	O
can	O
resurrect	O
itself	O
or	O
another	O
object	O
:	O
by	O
creating	O
a	O
reference	O
to	O
itself	O
in	O
an	O
object	O
that	O
it	O
can	O
reach	O
(	O
garbage	O
is	O
not	O
reachable	O
,	O
but	O
garbage	O
can	O
reference	O
non-garbage	O
objects	O
)	O
,	O
or	O
by	O
creating	O
a	O
reference	O
in	O
the	O
environment	O
(	O
global	O
variables	O
,	O
or	O
in	O
some	O
cases	O
static	B-General_Concept
variables	I-General_Concept
or	O
variables	O
in	O
a	O
closure	B-Language
)	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
for	O
an	O
object	O
to	O
resurrect	O
other	O
objects	O
if	O
both	O
are	O
being	O
collected	O
in	O
a	O
given	O
garbage	B-General_Concept
collection	I-General_Concept
cycle	O
,	O
by	O
the	O
same	O
mechanisms	O
.	O
</s>
<s>
In	O
the	O
above	O
examples	O
,	O
in	O
CPython	B-Language
prior	O
to	O
3.4	O
,	O
these	O
will	O
run	O
finalizers	B-Application
repeatedly	O
,	O
and	O
the	O
objects	O
will	O
not	O
be	O
garbage-collected	O
,	O
while	O
in	O
CPython	B-Language
3.4	O
and	O
later	O
,	O
the	O
finalizers	B-Application
will	O
only	O
be	O
called	O
once	O
,	O
and	O
the	O
objects	O
will	O
be	O
garbage-collected	O
the	O
second	O
time	O
they	O
become	O
unreachable	O
.	O
</s>
<s>
Object	B-General_Concept
resurrection	I-General_Concept
causes	O
a	O
large	O
number	O
of	O
problems	O
.	O
</s>
<s>
The	O
possibility	O
of	O
object	B-General_Concept
resurrection	I-General_Concept
means	O
that	O
the	O
garbage	B-General_Concept
collector	I-General_Concept
must	O
check	O
for	O
resurrected	O
objects	O
after	O
finalization	O
–	O
even	O
if	O
it	O
does	O
not	O
actually	O
occur	O
–	O
which	O
complicates	O
and	O
slows	O
down	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
In	O
some	O
circumstances	O
an	O
object	O
may	O
be	O
indestructible	O
:	O
if	O
an	O
object	O
is	O
resurrected	O
in	O
its	O
own	O
finalizer	B-Application
(	O
or	O
a	O
group	O
of	O
objects	O
resurrect	O
each	O
other	O
as	O
a	O
result	O
of	O
their	O
finalizers	B-Application
)	O
,	O
and	O
the	O
finalizer	B-Application
is	O
always	O
called	O
when	O
destroying	O
the	O
object	O
,	O
then	O
the	O
object	O
cannot	O
be	O
destroyed	O
and	O
its	O
memory	O
cannot	O
be	O
reclaimed	O
.	O
</s>
<s>
Thirdly	O
,	O
object	B-General_Concept
resurrection	I-General_Concept
may	O
be	O
unintentional	O
,	O
and	O
the	O
resulting	O
object	O
may	O
be	O
semantic	O
garbage	O
,	O
hence	O
never	O
actually	O
collected	O
,	O
causing	O
a	O
logical	O
memory	B-Error_Name
leak	I-Error_Name
.	O
</s>
<s>
A	O
resurrected	O
object	O
may	O
be	O
in	O
an	O
inconsistent	O
state	O
,	O
or	O
violate	O
class	B-Application
invariants	I-Application
,	O
due	O
to	O
the	O
finalizer	B-Application
having	O
been	O
executed	O
and	O
causing	O
an	O
irregular	O
state	O
.	O
</s>
<s>
In	O
some	O
languages	O
(	O
such	O
as	O
Java	O
and	O
Python	O
3.4	O
+	O
)	O
finalization	O
is	O
guaranteed	O
to	O
happen	O
exactly	O
once	O
per	O
object	O
,	O
so	O
resurrected	O
objects	O
will	O
not	O
have	O
their	O
finalizers	B-Application
called	O
;	O
therefore	O
,	O
resurrected	O
objects	O
must	O
execute	O
any	O
necessary	O
cleanup	O
code	O
outside	O
of	O
the	O
finalizer	B-Application
.	O
</s>
<s>
Languages	O
have	O
adopted	O
several	O
different	O
methods	O
for	O
coping	O
with	O
object	B-General_Concept
resurrection	I-General_Concept
,	O
most	O
commonly	O
by	O
having	O
two-phase	O
garbage	B-General_Concept
collection	I-General_Concept
in	O
the	O
presence	O
of	O
finalizers	B-Application
,	O
to	O
prevent	O
dangling	B-Error_Name
references	I-Error_Name
;	O
and	O
by	O
only	O
finalizing	O
objects	O
once	O
,	O
particularly	O
by	O
marking	O
objects	O
as	O
having	O
been	O
finalized	O
(	O
via	O
a	O
flag	O
)	O
,	O
to	O
ensure	O
that	O
objects	O
can	O
be	O
destroyed	O
.	O
</s>
<s>
Java	O
will	O
not	O
free	O
the	O
object	O
until	O
it	O
has	O
proven	O
that	O
the	O
object	O
is	O
once	O
again	O
unreachable	O
,	O
but	O
will	O
not	O
run	O
the	O
finalizer	B-Application
more	O
than	O
once	O
.	O
</s>
<s>
In	O
Python	O
,	O
prior	O
to	O
Python	O
3.4	O
,	O
the	O
standard	O
CPython	B-Language
implementation	O
would	O
treat	O
resurrected	O
objects	O
identically	O
to	O
other	O
objects	O
(	O
which	O
had	O
never	O
been	O
finalized	O
)	O
,	O
making	O
indestructible	O
objects	O
possible	O
.	O
</s>
<s>
Further	O
,	O
it	O
would	O
not	O
garbage	O
collect	O
cycles	O
that	O
contained	O
an	O
object	O
with	O
a	O
finalizer	B-Application
,	O
to	O
avoid	O
possible	O
problems	O
with	O
object	B-General_Concept
resurrection	I-General_Concept
.	O
</s>
<s>
Starting	O
in	O
Python	O
3.4	O
,	O
behavior	O
is	O
largely	O
the	O
same	O
as	O
Java	O
:	O
objects	O
are	O
only	O
finalized	O
once	O
(	O
being	O
marked	O
as	O
"	O
already	O
finalized	O
"	O
)	O
,	O
garbage	B-General_Concept
collection	I-General_Concept
of	O
cycles	O
is	O
in	O
two	O
phases	O
,	O
with	O
the	O
second	O
phase	O
checking	O
for	O
resurrected	O
objects	O
.	O
</s>
<s>
Objective-C	B-Language
2.0	I-Language
will	O
put	O
resurrected	O
objects	O
into	O
a	O
"	O
zombie	O
"	O
state	O
,	O
where	O
they	O
log	O
all	O
messages	O
sent	O
to	O
them	O
,	O
but	O
do	O
nothing	O
else	O
.	O
</s>
<s>
See	O
also	O
Automatic	O
Reference	O
Counting	O
:	O
Zeroing	O
Weak	B-General_Concept
References	I-General_Concept
for	O
handling	O
of	O
weak	B-General_Concept
references	I-General_Concept
.	O
</s>
<s>
In	O
the	O
.NET	B-Application
Framework	I-Application
,	O
notably	O
C#	O
and	O
VB.NET	O
,	O
object	O
finalization	O
is	O
determined	O
by	O
a	O
finalization	O
"	O
queue	O
"	O
,	O
which	O
is	O
checked	O
during	O
object	O
destruction	O
.	O
</s>
<s>
Objects	O
with	O
a	O
finalizer	B-Application
are	O
placed	O
in	O
this	O
queue	O
on	O
creation	O
,	O
and	O
dequeued	O
when	O
the	O
finalizer	B-Application
is	O
called	O
,	O
but	O
can	O
be	O
manually	O
dequeued	O
(	O
prior	O
to	O
finalization	O
)	O
with	O
SuppressFinalize	O
or	O
re-enqueued	O
with	O
ReRegisterForFinalize	O
.	O
</s>
<s>
Thus	O
by	O
default	O
objects	O
with	O
finalizers	B-Application
are	O
finalized	O
at	O
most	O
once	O
,	O
but	O
this	O
finalization	O
can	O
be	O
suppressed	O
,	O
or	O
objects	O
can	O
be	O
finalized	O
multiple	O
times	O
if	O
they	O
are	O
resurrected	O
(	O
made	O
accessible	O
again	O
)	O
and	O
then	O
re-enqueued	O
for	O
finalization	O
.	O
</s>
<s>
Further	O
,	O
weak	B-General_Concept
references	I-General_Concept
by	O
default	O
do	O
not	O
track	O
resurrection	O
,	O
meaning	O
a	O
weak	B-General_Concept
reference	I-General_Concept
is	O
not	O
updated	O
if	O
an	O
object	O
is	O
resurrected	O
;	O
these	O
are	O
called	O
short	O
weak	B-General_Concept
references	I-General_Concept
,	O
and	O
weak	B-General_Concept
references	I-General_Concept
that	O
track	O
resurrection	O
are	O
called	O
long	O
weak	B-General_Concept
references	I-General_Concept
.	O
</s>
<s>
Object	B-General_Concept
resurrection	I-General_Concept
is	O
useful	O
to	O
handle	O
an	O
object	O
pool	O
of	O
commonly	O
used	O
objects	O
,	O
but	O
it	O
obscures	O
code	O
and	O
makes	O
it	O
more	O
confusing	O
.	O
</s>
<s>
With	O
object	B-General_Concept
resurrection	I-General_Concept
,	O
a	O
pooling	O
technique	O
would	O
reduce	O
the	O
unnecessary	O
overhead	O
of	O
creation	O
and	O
destruction	O
.	O
</s>
