WeakEventManager has a subscriber link

I use WeakEventManager to avoid memory leaks, and I started to overuse them. I created extension methods, for example for INotifyPropertyChanged, for example:


public static void AddWeakPropertyChanged(this INotifyPropertyChanged item, Action handler)
{
    PropertyChangedEventManager.AddHandler(item, (s, e) => handler(e.PropertyName), string.Empty);
}

Now I quickly realized that this did not work. In fact, you cannot use anonymous methods for poorly handling events. (If I understand correctly, the compiler creates a “closure class” (for storing reference values) for it, which has a handler, but since your closure class is not mentioned anywhere, GC will clear it and the event handler will not be called)

Question # 1: Is this correct? I mean, this is correct, then when using an anonymous method (or lambda) for a weak event handler, the handler is called only if the GC did not execute at the same time (for example, is it undefined)?

Well, I try so hard, so I did some unit tests to make sure everything was correct. It would seem that this is true until I click on the following unit test:


        class DidRun
        {
            public bool Value { get; set; }
        }
        class TestEventPublisher
        {
            public event EventHandler<EventArgs> MyEvent;
            public void RaiseMyEvent()
            {
                if (MyEvent != null)
                    MyEvent(this, EventArgs.Empty);

            }
        }
        class TestClosure
        {
            public DidRun didRun { get; set; }
            public EventHandler<EventArgs> Handler { get; private set; }
            public TestClosure()
            {
                this.Handler = new EventHandler<EventArgs>((s, e) => didRun.Value = true);
            }
        }
        [TestMethod]
        public void TestWeakReference()
        {
            var raiser = new TestEventPublisher();
            var didrun = new DidRun();
            var closure = new TestClosure { didRun = didrun };
            WeakEventManager<TestEventPublisher, EventArgs>.AddHandler(raiser, "MyEvent", closure.Handler);
            closure = null;

            GC.Collect();
            GC.Collect();
            raiser.RaiseMyEvent();
            Assert.AreEqual(false, didrun.Value);
        }

Question # 2: Can someone explain to me why this test fails?

Expectation: here I have no closures (I pulled them out to make sure what is happening), I just have an object (closure) that subscribes to the event with WeakEventManager, and then I delete the link to it (closure = null;).

2 GC.Collect(), , WeakEventManager , . ?

EDIT: , ,

+5
1

, GC , , .

unit test TestClosure, WeakEventManager, TestClosure. , ...

, :

class DidRun
{
    public bool Value { get; set; }
}

class TestEventPublisher
{
    public event EventHandler<EventArgs> MyEvent;
    public void RaiseMyEvent()
    {
        if (MyEvent != null)
            MyEvent(this, EventArgs.Empty);
    }
}

class TestClosure
{
    static public EventHandler<EventArgs> Register(TestEventPublisher raiser, DidRun didrun)
    {
        EventHandler<EventArgs> handler = (s, e) => didrun.Value = true;
        WeakEventManager<TestEventPublisher, EventArgs>.AddHandler(raiser, "MyEvent", handler);
        return handler;
    }
}

[TestMethod]
public void Test1()
{
    var raiser = new TestEventPublisher();
    var didrun = new DidRun();

    TestClosure.Register(raiser, didrun);

    // The reference to the closure 'handler' is not being held,
    //  it may or may not be GC'd (indeterminate result)

    raiser.RaiseMyEvent();
    Assert.IsTrue(didrun.Value);
}

[TestMethod]
public void Test2()
{
    var raiser = new TestEventPublisher();
    var didrun = new DidRun();

    // The reference to the closure 'handler' is not being held, it GC'd
    TestClosure.Register(raiser, didrun);

    GC.Collect();
    GC.Collect();

    raiser.RaiseMyEvent();
    Assert.IsFalse(didrun.Value);
}

[TestMethod]
public void Test3()
{
    var raiser = new TestEventPublisher();
    var didrun = new DidRun();

    // Keep local copy of handler to prevent it from being GC'd
    var handler = TestClosure.Register(raiser, didrun);

    GC.Collect();
    GC.Collect();

    raiser.RaiseMyEvent();
    Assert.IsTrue(didrun.Value);
}

, (), GC'd. A ConditionalWeakTable :

// ConditionalWeakTable will hold the 'value' as long as the 'key' is not marked for GC
static private ConditionalWeakTable<INotifyPropertyChanged, EventHandler<PropertyChangedEventArgs>> _eventMapping =
  new ConditionalWeakTable<INotifyPropertyChanged, EventHandler<PropertyChangedEventArgs>>();

public static void AddWeakPropertyChanged(this INotifyPropertyChanged item, Action<string> handlerAction)
{
    EventHandler<PropertyChangedEventArgs> handler;

    // Remove any existing handler for this item in case it registered more than once
    if (_eventMapping.TryGetValue(item, out handler))
    {   
        _eventMapping.Remove(item);
        PropertyChangedEventManager.RemoveHandler(item, handler, string.Empty);
    }   

    handler = (s, e) => handlerAction(e.PropertyName);

    // Save handler (closure) to prevent GC
    _eventMapping.Add(item, handler);

    PropertyChangedEventManager.AddHandler(item, handler, string.Empty);
}

class DidRun
{
    static public string Value { get; private set; }
    public void SetValue(string value) { Value = value; }
}

[TestMethod]
public void Test4()
{
    var property = new ObservableObject<string>();

    var didrun = new DidRun();
    property.AddWeakPropertyChanged(
        (x) => 
        {
            didrun.SetValue("Property Name = " + x);
        });

    GC.Collect();
    GC.Collect();

    property.Value = "Hello World";

    Assert.IsTrue(DidRun.Value != null);
}
+3
source

All Articles