Forum: JRuby finalizers be called at interrupt?

Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2012-06-14 21:53
Hello all.

I noticed that in MRI, this code:

def finalized_puts bad, benign
    ObjectSpace.define_finalizer(bad) { puts benign.to_s }
end

n = 0
loop {
  a = [3]
  finalized_puts a, n
  n += 1
  print '.'
}

If I hit 'ctrl-c' to interrupt it, in MRI the finalizers are run, but no
in Jruby.  Is this expected?  Will file a bug report eventually if no
response.
Cheers!
-roger-
5993756d2b69ce0bc16df3318c240d54?d=identicon&s=25 Wayne Meissner (Guest)
on 2012-06-14 23:05
(Received via mailing list)
References won't be collected immediately upon interrupt (and
therefore the finalizers won't be run) - it'll happen when the GC
needs to reclaim memory.

But, won't your finalized_puts bind the 'bad' param in the block, and
stop it from being collected? (i.e. the block will always have a ref
to the bad instance, and won't allow it to be collected).

if you change it to something like this, it might work:

def finalized_puts bad, benign
  ObjectSpace.define_finalizer(bad, create_finalizer(benign))
end

def create_finalizer(benign)
  Proc.new { |bad| puts benign.to_s }
end
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.