Skip to content

Instantly share code, notes, and snippets.

@niamster
Created April 6, 2015 20:38
Show Gist options
  • Save niamster/e050dbee1ace792d126b to your computer and use it in GitHub Desktop.
Save niamster/e050dbee1ace792d126b to your computer and use it in GitHub Desktop.
Method missing leak
#!/usr/bin/env ruby
require 'weakref'
$LEAK = true
class Hidden
def initialize(cls)
@cls = cls
end
if $LEAK
def method_missing(meth, *args, &block)
@cls.public_send(meth, *args, &block)
end
else
def foo
@cls.foo
end
end
end
class Test
attr_reader :foo
def initialize(foo)
@foo = foo
end
end
test = Hidden.new(Test.new('foo'))
p test.foo
weak = WeakRef.new test
test = nil
puts 'Wait'
tests = []
loop do
GC.start
break unless weak.weakref_alive?
tests << Test.new(nil)
end
puts 'PASS' unless weak.weakref_alive?
sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment