Opposite of initialize

P

peter.meier998

Hi,

I'm looking for the opposite function of initialize() as I need to
call something before an object is "destroyed".


Thanks
Peter
 
E

Eleanor McHugh

I'm looking for the opposite function of initialize() as I need to
call something before an object is "destroyed".

ObjectSpace#define_finalizer allows you to have a proc executed when
an object is about to be destroyed. If I remember rightly this is when
the object is actually garbage collected, which can be some time after
all its references have been released.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
 
P

peter.meier998

Thanks. Exacalty what I need. Haven't seen it.

ObjectSpace#define_finalizer allows you to have a proc executed when  
an object is about to be destroyed. If I remember rightly this is when  
the object is actually garbage collected, which can be some time after  
all its references have been released.

Ellie

Eleanor McHugh
Games With Brainshttp://slides.games-with-brains.net
 
R

Robert Klemme

ObjectSpace#define_finalizer allows you to have a proc executed when
an object is about to be destroyed. If I remember rightly this is when
the object is actually garbage collected, which can be some time after
all its references have been released.

And the finalizer is called *after* the object has gone! Peter, please
do also note that there are some subtle issues lurking. One important
thing to remember is that you must not create the finalizer in an
instance method (such as #initialize) because it will bind "self" and
thusly prevent GC of the instance.

Kind regards

robert
 
H

Hassan Schroeder

And the finalizer is called *after* the object has gone!

So "before" it's destroyed and "after" it's destroyed are two different
things.

ObjectSpace#finalizer wouldn't seem to fit the original request, if the
OP is looking to actually *do* anything with the object (retrieve some
attribute or whatever).

Curious whether there's something else that would enable that case.
 
R

Robert Klemme

So "before" it's destroyed and "after" it's destroyed are two different
things.
Absolutely!

ObjectSpace#finalizer wouldn't seem to fit the original request, if the
OP is looking to actually *do* anything with the object (retrieve some
attribute or whatever).

Curious whether there's something else that would enable that case.

You can use the mechanism I wrote about recently[1] which is not exactly
the same as a finalizer but can ensure timely resource deallocation. In
a way it's even better because you get immediate cleanup when the
resource is not used any more and not delayed cleanup (i.e. when the
garbage collector decides that he wants to collect the instance).

Kind regards

robert


[1]
http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html
 
7

7stud --

Hassan said:
So "before" it's destroyed and "after" it's destroyed are two different
things.

Not only that:

1) "it goes out of scope and there are no more references to the object
so the object is ready to be garbage collected"

and

2) "the object is actually garbage collected and destroyed"

are two different things. The actual destruction may happen only after
your program ends.
 
R

Rick DeNatale

Not only that:

1) "it goes out of scope and there are no more references to the object
so the object is ready to be garbage collected"

and

2) "the object is actually garbage collected and destroyed"

are two different things. =A0The actual destruction may happen only after
your program ends.

In every GCd language I've used, including Smalltalk, Java and Ruby,
finalization has always turned out to be an unreliable mechanism for
doing resource cleanup.

The role of the GC is actually to make sure that objects don't get
freed prematurely, a GC's primary job is to prevent 'dangling' pointer
problems. Reclaiming memory is a secondary goal. So there's no
guarantee that an object will be freed as soon as it's no longer
reference-able. And as you point out, objects might only be reclaimed
by the OS after the program exits.

Over the past 25 years or so, I've come to regard finalization as an
"attractive nuisance."

--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top