Kent Dahl <kentda / stud.ntnu.no> wrote in message news:<3DEC8000.C1621D0A / stud.ntnu.no>...
> I'll bet you a beer your barking up the wrong tree, but:
> What version of Ruby are you using?

ruby 1.7.3 (2002-11-26) [i686-linux]

> Can you give us a complete, runnable program (preferably as short as
> possible) that reproduces the bug? 

Below is an extremely simplified concept of the application that I'm
building. I have lots and lots of Foo objects which I then clone and
put them in a Bar object. For this purpose I only clone a single Foo
object and put it in a Bar object.

If I clone aFoo using "aFoo.clone", then the finalizers won't kick in
(and no, the finalizer proc doesn't keep a reference of the object,
it's justs printing some string).

But if I "clone" aFoo using "Foo.new(aFoo.stuff)", then the finalizer
will kick in.

What gives?

================================ C U T
===================================
#!/usr/bin/env ruby

class Foo
	attr_accessor :stuff
	def initialize stuff
		@stuff = stuff

		ObjectSpace.define_finalizer(self, Foo.create_finalizer)
	end

	def Foo.create_finalizer
		proc { |id|
			printf ">>>> Finalizer for Foo on 0x%x <<<<\n", id
		}
	end
end

class Bar
	attr_accessor :stuff, :my_foos
	def initialize stuff, my_foos
		@stuff = stuff
		@my_foos = my_foos

		ObjectSpace.define_finalizer(self, Bar.create_finalizer)
	end

	def Bar.create_finalizer
		proc { |id|
			printf ">>>> Finalizer for Bar on 0x%x <<<<\n", id
		}
	end
end

# Generate a Foo
aFoo = Foo.new(1)

# Generate some Bars with Foo clones
i = 0
while true
	i += 1	
	# GC won't collect aFoo's clone
	#foo_clone = aFoo.clone

	# GC will collect fresh Foo instance
	foo_clone = Foo.new(aFoo.stuff)

	# Create compound Bar object with aFoo's clone
	bar = Bar.new(i, foo_clone)
	
	# Do complex stuff with bar
	puts "#{i}: #{bar}"
end
================================ C U T ===============================