It doesn't produce any output!

Y

Yusuf Celik

Hi,
I wonder if something is wrong with the following code.
Because, it doesn't produce any output.
BTW, I am new to Ruby.

Regards
yc


include ObjectSpace

a=b=c="A"

define_finalizer(a, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })
define_finalizer(b, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })
define_finalizer(c, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })
 
R

Robert Klemme

Hi,
I wonder if something is wrong with the following code.
Because, it doesn't produce any output.
BTW, I am new to Ruby.

Regards
yc


include ObjectSpace

a=b=c="A"

define_finalizer(a, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })
define_finalizer(b, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })
define_finalizer(c, proc {|id| puts "Finalizer
#{ObjectSpace._id2ref(id)} on #{id}" })

Your finalizers lock the object in memory. You can find a recent
discussion about the topic in the archives.

Please note also that you are defining three finalizers for a single
instance which is probably not what you want.

Kind regards

robert
 
Y

Yusuf Celik

Thanks Robert,

But, without #{ObjectSpace._id2ref(id)} no problem.
You think this statement locks the object in memory.

Thanks again
yc
 
R

Robert Klemme

Thanks Robert,

But, without #{ObjectSpace._id2ref(id)} no problem.
You think this statement locks the object in memory.

No, that's not the one. The mere fact that you define a proc / lambda
in the presence of a local variable that references the object is
keeping the object around. The proc is a closure and keeps a reference
to a even if a goes out of scope (which it does not in your case).

robert
 
C

Chris Carter

Thanks Robert,

But, without #{ObjectSpace._id2ref(id)} no problem.
You think this statement locks the object in memory.

Thanks again
yc

it is pretty well documented that you cannot do that. From the docs
of ObjectSpace._id2ref:

Converts an object id to a reference to the object. May not be
called on an object id passed as a parameter to a finalizer.
 

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,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top