It doesn't produce any output!

Discussion in 'Ruby' started by Yusuf Celik, May 2, 2007.

  1. Yusuf Celik

    Yusuf Celik Guest

    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}" })

    --
    Posted via http://www.ruby-forum.com/.
     
    Yusuf Celik, May 2, 2007
    #1
    1. Advertising

  2. On 02.05.2007 16:50, Yusuf Celik wrote:
    > 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
     
    Robert Klemme, May 2, 2007
    #2
    1. Advertising

  3. Yusuf Celik

    Yusuf Celik Guest

    Thanks Robert,

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

    Thanks again
    yc

    Robert Klemme wrote:
    > 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



    --
    Posted via http://www.ruby-forum.com/.
     
    Yusuf Celik, May 3, 2007
    #3
  4. On 03.05.2007 09:18, Yusuf Celik wrote:
    > 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
     
    Robert Klemme, May 3, 2007
    #4
  5. Yusuf Celik

    Chris Carter Guest

    On 5/3/07, Yusuf Celik <> wrote:
    > Thanks Robert,
    >
    > But, without #{ObjectSpace._id2ref(id)} no problem.
    > You think this statement locks the object in memory.
    >
    > Thanks again
    > yc
    >
    > Robert Klemme wrote:
    > > 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

    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    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.

    --
    Chris Carter
    concentrationstudios.com
    brynmawrcs.com
     
    Chris Carter, May 3, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    1,470
  2. Replies:
    5
    Views:
    465
    Luc The Perverse
    Oct 27, 2006
  3. Protoman
    Replies:
    2
    Views:
    355
    Karl Heinz Buchegger
    Nov 10, 2005
  4. Vasu
    Replies:
    2
    Views:
    599
    Knute Johnson
    Oct 18, 2008
  5. helzer
    Replies:
    1
    Views:
    110
    Philip Hallstrom
    Oct 25, 2006
Loading...

Share This Page