i searched the wiki and comp.lang.ruby and found several examples of using
finalizers. strangely, though they work for some people, not all work for me :
#!/usr/bin/env ruby
class Test
def Test.finalizer
Proc.new{ puts “finalizing” }
end
def initialize
ObjectSpace.define_finalizer(self, Test.finalizer)
end
end
t = Test.new
class A
def initialize
ObjectSpace.define_finalizer(self, proc{|id| puts “final #{id}”})
end
end
a = A.new
outputs only
“finalizing”
thus the second technique does not work…
here’s my setup
/tmp > uname -a
Linux 2.4.2-2 #1 SMP Fri Nov 23 20:51:15 GMT 2001 i686 unknown
i found quite a few references that claim the finalizing proc may not contain
references to the object in question, since it would therefore, never be
candidate for gc… my experiements show this to be true, but ‘the book’ seems
to think otherwise… what’s the deal?
from the book:
define_finalizer
ObjectSpace.define_finalizer( anObject, aProc=proc() )
Adds aProc as a finalizer, to be called when anObject is about to be
destroyed.
note the ‘about’ part. is this correct?
thanks for any insight.
-ara
···
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
i found quite a few references that claim the finalizing proc may not
contain references to the object in question, since it would therefore,
never be candidate for gc... my experiements show this to be true,
yes, this is true
but 'the book' seems to think otherwise... what's the deal?
'the book' was written for 1.6.0, before this was corrected
thus, there is no way to impliment the following logic :
if being_garbage_collected
self.free self.a_resource
end
this seems unfortunate, though i have not yet come across a definite need for
it in ruby, i have used a similar technique in perl before to decrement a
counter in a multi-threaded application
-a
···
On Thu, 31 Oct 2002, ts wrote:
i found quite a few references that claim the finalizing proc may not
contain references to the object in question, since it would therefore,
never be candidate for gc… my experiements show this to be true,
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
thus, there is no way to impliment the following logic :
if being_garbage_collected
self.free self.a_resource
end
No way, but if you think to the GC you'll see that it's a Good Thing (TM)
Imagine that your method re-activate the object when ruby try to
garbage it.
this seems unfortunate, though i have not yet come across a definite need for
it in ruby, i have used a similar technique in perl before to decrement a
counter in a multi-threaded application
Well, perl don't have a real GC. If I remember it correctly, matz has made
*volontary* the choice to make *difficult* to use such technique because
they are too error prone.