Unlocking the Secrets of Managed memory: Dive into event handler leak insights!
-
Event handler leaks have been around for a long time, and they are one of the peskiest issues WPF (Windows Presentation Foundation) developers regularly deal with.
But .NET *never* leaks memory, right?
-
Event handler leaks have been around for a long time, and they are one of the peskiest issues WPF (Windows Presentation Foundation) developers regularly deal with.
But .NET *never* leaks memory, right?
The problem is that the memory isn't "leaked" if someone, or anyone, has a reference to it. The publisher / event has. If .net decided to garbage collect the subscriber, the event would be left with a (non-null) invalid pointer. As soon as the event (or rather: the last event) referencing the subscriber, dotNet would garbage collect it. I really would feel uncomfortable with dotNet garbage collecting an object still referenced.
-
The problem is that the memory isn't "leaked" if someone, or anyone, has a reference to it. The publisher / event has. If .net decided to garbage collect the subscriber, the event would be left with a (non-null) invalid pointer. As soon as the event (or rather: the last event) referencing the subscriber, dotNet would garbage collect it. I really would feel uncomfortable with dotNet garbage collecting an object still referenced.