Forum: Ruby Opposite of initialize

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3c477e6a8717fb4f98f720c4e3ccce20?d=identicon&s=25 unknown (Guest)
on 2009-04-28 18:45
(Received via mailing list)
Hi,

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


Thanks
   Peter
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-28 18:53
(Received via mailing list)
2009/4/28 <peter.meier998@gmail.com>

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



http://ruby-doc.org/core/classes/ObjectSpace.html#M006791
Fa2521c6539342333de9f42502657e5a?d=identicon&s=25 Eleanor McHugh (Guest)
on 2009-04-28 18:56
(Received via mailing list)
On 28 Apr 2009, at 17:45, peter.meier998@gmail.com wrote:
> 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
----
raise ArgumentError unless @reality.responds_to? :reason
3c477e6a8717fb4f98f720c4e3ccce20?d=identicon&s=25 unknown (Guest)
on 2009-04-28 19:06
(Received via mailing list)
Thanks. Exacalty what I need. Haven't seen it.

On Apr 28, 5:55 pm, Eleanor McHugh <elea...@games-with-brains.com>
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-28 19:11
(Received via mailing list)
On 28.04.2009 18:55, Eleanor McHugh wrote:
> On 28 Apr 2009, at 17:45, peter.meier998@gmail.com wrote:
>> 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.

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
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-04-28 19:29
(Received via mailing list)
On Tue, Apr 28, 2009 at 10:10 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:

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

> 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-28 23:51
(Received via mailing list)
On 28.04.2009 19:28, Hassan Schroeder wrote:
> On Tue, Apr 28, 2009 at 10:10 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>
>>>> I'm looking for the opposite function of initialize() as I need to
>>>> call something before an object is "destroyed".
>
>> And the finalizer is called *after* the object has gone!
>
> 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/00...
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-29 01:42
Hassan Schroeder wrote:
> On Tue, Apr 28, 2009 at 10:10 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>
>>>> I'm looking for the opposite function of initialize() as I need to
>>>> call something before an object is "destroyed".
>
>> And the finalizer is called *after* the object has gone!
>
> 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.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-04-29 19:13
(Received via mailing list)
On Tue, Apr 28, 2009 at 7:42 PM, 7stud -- <bbxx789_05ss@yahoo.com>
wrote:
>> things.
>
> are two different things.  The 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."

--
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
This topic is locked and can not be replied to.