Created
April 6, 2015 20:38
-
-
Save niamster/e050dbee1ace792d126b to your computer and use it in GitHub Desktop.
Method missing leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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