Aren't routed events weak ???

16 views
Skip to first unread message

Marlon Grech

unread,
Nov 3, 2009, 7:37:55 PM11/3/09
to wpf-di...@googlegroups.com
Hey guys,

I was always under the impression that routed events follow the weak event pattern... today I am doing some stuff and I found out that they are not. Looked up in reflector and found out this

internal RoutedEventHandlerInfo(Delegate handler, bool handledEventsToo)
{
    this._handler = handler;
    this._handledEventsToo = handledEventsToo;
}


It's actually keeping a striong reference to the handler.

I tried something out and it is true objects don't get GCed

 // Add 1000 objects. If routed events are "weak" thiese will get garbage
            // collected afterwards
            for (int i = 0; i < 1000; i++)
            {

                TestObject o = new TestObject();
               
                // The below line causes the memory leak
                ((Button)sender).Click += o.Handler;

               

            }


            // o should be finalized here
            GC.Collect();

Am I missing something?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

Josh Smith

unread,
Nov 3, 2009, 7:45:42 PM11/3/09
to wpf-di...@googlegroups.com
I never read anything that suggested routed events are weak.  I think that your use case is valid (certainly not sane, but valid ;).  The scope of a variable doesn't determine its lifetime when it is an event sync.

By the way, the RoutedEventHandlerInfo class instances might be weakly referenced, which would obviate the need for it store store a weakref to the handler.

Josh

Eric Burke

unread,
Nov 3, 2009, 7:48:02 PM11/3/09
to wpf-di...@googlegroups.com
Did you attach this to a profiler?  My guess is that even if those objects aren't collected yet, they are eligible for it.  If you post your whole solution/project we can dig into it.

Also, if your TestObject has a finalizer, it will automatically get promoted to Gen1 and get collected later.

Josh Smith

unread,
Nov 3, 2009, 7:57:31 PM11/3/09
to wpf-di...@googlegroups.com
I might have to retract my initial reply...

Marlon Grech

unread,
Nov 3, 2009, 8:12:01 PM11/3/09
to wpf-di...@googlegroups.com
here you go...

attached is a sample


Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App



WeakRoutedEvents.zip.doc

Eric Burke

unread,
Nov 4, 2009, 12:01:37 AM11/4/09
to wpf-di...@googlegroups.com
This is what I suspected but I wanted to prove it with the profiler.

The upshot is that Application.Current(which is a static and therefore a GC root).MainWindow = your window.  Your window references the Button.  The Button references each TestObject via the Button.Click delegate.

See attached image for the exact root path.

This is a perfect example of why you always need to unhook your handlers, even if you are absolutely-postively-there's-no-possible-way-this-could-ever-cause-an-issue sure.  Too much stuff happens under the hood of a WPF app that you can't always see from above.


memory.png

Paul Stovell

unread,
Nov 4, 2009, 12:22:14 AM11/4/09
to wpf-di...@googlegroups.com
Hi Marlon,

Wouldn't this be a bad thing if they were collected?

button.Click += x
...
button.DoClick();
// sometimes, almost randomly, nothing happens because the GC decided to run

As a developer I would find this unexpected and annoying, expecially because it would happen what appears to be 'randomly'. I'm unlikely to discover this in development and I'll have to wait for a bug report 3 months later.

Also I don't think it's such a problem. If the button keeps a reference to your object, that's not so bad since when you close the window it all gets cleaned up anyway. It would be much worse if you had a static event somewhere else that your Window subscribed to, since that would result in the Window being kept alive.

Paul
--
Paul Stovell

Brennon Williams

unread,
Nov 5, 2009, 4:52:27 AM11/5/09
to wpf-di...@googlegroups.com

Your post reminded me of this that I seen a while back...

 

http://www.codeproject.com/KB/showcase/IfOnlyWedUsedANTSProfiler.aspx?msg=2323817

Sacha Barber

unread,
Nov 5, 2009, 5:13:42 AM11/5/09
to wpf-di...@googlegroups.com
Ha ha
--
Sacha Barber
sacha....@gmail.com
Reply all
Reply to author
Forward
0 new messages