Handling events
One of the most common causes of memory leaks appearing in an application is the failure to remove event handlers once objects are no longer needed. When we attach an event handler to an object's event in the usual way, we are effectively passing that object a reference to the handler and creating a hard reference to it.
When the object is no longer needed and could otherwise be disposed of, the reference in the object that raises the event will prevent that from occurring. This is because the garbage collector cannot collect an object that can be accessed from any part of the application code. In the worst case scenario, the object being kept alive may contain numerous other objects and so inadvertently keep them alive also.
The ...
Get Mastering Windows Presentation Foundation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.