Hi,

Sorry for the multiple posts on this matter, but i did a little more
digging.

If I change the original code to include the following function:

  def clear_handler
    if @wc.is_alive
      @wc.target.text_changed.remove @handler
      @handler = nil # without this line, the Tester will still leak
    end
  end

as noted in the comments if you comment out the @handler = nil line,
@wc.target will still leak.

Even more curious, if we just do this:

  def add_handler
    @handler = lambda {|*a| puts "text changed" }
  end

the code doesnt leak at all.  It seems to be specifically adding the proc to
an event handler and then subsequently keeping that proc alive that causes
this issue.  Curious...

Thanks again for the help!  I'm ammending the Codeplex bug.

Martin


On Mon, Aug 24, 2009 at 9:13 AM, Tomas Matousek <
[email protected]> wrote:

> I’ve filed this issue as
> http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=2067.
>
>
>
> Tomas
>
>
>
> *From:* [email protected] [mailto:
> [email protected]] *On Behalf Of *Martin Smith
> *Sent:* Friday, August 21, 2009 2:48 PM
> *To:* [email protected]
> *Subject:* [Ironruby-core] Issues with using Procs for events
>
>
>
> Hello,
>
>
>
> I am having some object lifetime issues with events in IronRuby.  I think I
> have a repro, so bear with me.  The problem seems to be that the proc seems
> to have a bunch of extra "stuff" in its execution environment.
>
>
>
> Consider the following ruby code:
>
>
>
> require "System.Windows.Forms"
>
> include System::Windows::Forms
>
> include System
>
>
>
> class Tester
>
>   attr_reader :wc
>
>
>
>
>
>   def test
>
>     @wc = WeakReference.new(Button.new)
>
>     add_handler
>
>   end
>
>
>
>   def add_handler
>
>     # if we uncomment the following two lines out, and comment out the last
> line, the Button will leak and not
>
>     # be cleaned up even though there should be no live references to it
>
>     #...@handler = lambda {|*a| puts "text changed" }
>
>     #[email protected]_changed.add <%[email protected]_changed.add> @handler
>
>     @wc.target.text_changed.add lambda {|*a| puts "text changed" }
>
>   end
>
> end
>
>
>
> t = Tester.new
>
> t.test
>
> System::GC.collect
>
> puts "wc is alive = #{t.wc.is_alive}" # this should always print false
>
>
>
>
>
> If you uncomment the two lines indicated in add_handler, the button in the
> weak reference will leak. I think that this is incorrect.  The proc object
> (and its associated binding) shouldn't have any live references to
> @wc.target.  I'm not 100% sure, but this makes it a bit harder if we want to
> be able to detach the proc later.
>
>
>
> Any thoughts?
>
> Thanks!
>
> Martin
>
> _______________________________________________
> Ironruby-core mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
>
_______________________________________________
Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to