Robert Klemme
1/17/2007 9:50:00 PM
On 17.01.2007 22:00, paul.denize@datacom.co.nz wrote:
> I have Ruby 182-15 on WinXP
>
> When I run the code below only the first finalizer runs. The others
> two don't seem to run. I found similar posts on other sites, but no
> solutions.
>
> You can duplicate the first and it runs twice (so two finalizers is
> ok).
>
> To me they are all somewhat identical. Can anyone tell me why it does
> not work.
>
> I have a number of machines that run testware in ruby 152 so I'd like
> to KNOW that upgrading will fix the problem before taking this path.
>
>
> Paul
>
> ------- Test.rb --------
>
> class MyClass
>
> def initialize
>
> ObjectSpace.define_finalizer(self,self.class.method(:finalize).to_proc)
> ObjectSpace.define_finalizer(self,proc{|id| puts "proc final
> #{id}"})
> ObjectSpace.define_finalizer(self,self.dofinalize)
> end
>
> def self.finalize(id)
> puts "Object #{id} dying at #{Time.new}"
> end
>
> def self.f2(id)
> puts "F2 #{id}"
> end
>
> def dofinalize
> return Proc.new { |param|
> puts "Doing finalize #{param[0]}"
> }
> end
> end
>
>
> MyClass.new
> ObjectSpace.garbage_collect
>
> puts "DONE"
>
> -------- output -------
>
>> Test.rb
> DONE
> Object 20691624 dying at Thu Jan 18 09:55:38 New Zealand Standard Time
> 2007
>
There's a subtle difference between the first and the other two: the
first one does not create a closure which binds "self" to the current
instance. That might be an explanation.
Kind regards
robert