[IronPython] Event unhooking and IPy2

Marty Nelson Marty.Nelson at symyx.com
Tue Dec 2 20:11:13 CET 2008


We've had probably with memory leaks on Windows Form subscribed menu
item clicks, just with plain vanilla C#.

Check out this link http://www.bobpowell.net/eventsubscribers.htm (There
are some additional steps for System.Windows.Forms.Component), and
here's some sample code I wrote in C# that you could leverage:

    [TestClass]
    public class UnhookEventsTests
    {
        private class Item
        {
            public event EventHandler Clicked;

            public void FireEvent()
            {
                if(Clicked != null)
                {
                    Clicked(this, EventArgs.Empty);
                }
            }
        }

        [TestMethod]
        public void UnhookEvents()
        {
            Item item = new Item();
            item.Clicked += Clicked;
            item.Clicked += Clicked2;

            item.FireEvent();

            Assert.AreEqual(2, callCount_);

            FieldInfo field = typeof (Item).GetField("Clicked",
BindingFlags.NonPublic | BindingFlags.Instance);
            Delegate del = (Delegate) field.GetValue(item);
            Delegate[] list = del.GetInvocationList();

            Assert.AreEqual(2, list.Length);

            //This works:
            //foreach (Delegate d in list)
            //{
            //    item.Clicked -= (EventHandler) d;
            //}

            //This is more generic and avoids the cast to the specific
handler type
            EventInfo eventInfo = typeof (Item).GetEvent("Clicked");
            foreach (Delegate d in list)
            {
                eventInfo.RemoveEventHandler(item, d);
            }

            item.FireEvent();

            Assert.AreEqual(2, callCount_);
        }
        private int callCount_;
        private void Clicked(object sender, EventArgs e)
        {
            callCount_++;
        }        
        
        private void Clicked2(object sender, EventArgs e)
        {
            callCount_++;
        }
    }

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Kamil
Dworakowski
Sent: Saturday, November 29, 2008 1:16 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Event unhooking and IPy2

On Fri, Nov 28, 2008 at 11:39 AM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> Just as a follow up to this - the reason for the hack below is that
> subscribing to events from IronPython is still causing our UI objects
(and
> their whole object graphs) to not be garbage collected.

To be fair, we have no reason to belive it is IronPython's fault. The
bug that event handlers would prevent garbage collection was fixed,
and we were able to remove *some* of the event nuking.
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

=======
Notice: This e-mail message, together with any attachments, contains
information of Symyx Technologies, Inc. or any of its affiliates or
subsidiaries that may be confidential, proprietary, copyrighted,
privileged and/or protected work product, and is meant solely for
the intended recipient. If you are not the intended recipient, and
have received this message in error, please contact the sender
immediately, permanently delete the original and any copies of this
email and any attachments thereto.



More information about the Ironpython-users mailing list