The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Collecting event handler / delegate garbage

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frans Bouma

Posts: 265
Nickname: fbouma
Registered: Aug, 2003

Frans Bouma is a senior software engineer for Solutions Design
Collecting event handler / delegate garbage Posted: Aug 1, 2003 8:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Frans Bouma.
Original Post: Collecting event handler / delegate garbage
Feed Title: Frans Bouma's blog
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/fbouma/Rss.aspx
Feed Description: Generator.CreateCoolTool();
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Frans Bouma
Latest Posts From Frans Bouma's blog

Advertisement

The .NET's garbage collector (GC) is a wonderful thing: you simply instantiate objects and when they're out of scope and / or no reference is known to those instances, they get cleaned up and you don't have to worry.

At least... in most cases. There are situations where you might think the objects you left behind get cleaned up but instead are kept in memory because there is still a reference known to those objects so the GC will not clean them up. In other words: the objects can't be 'marked for garbage collection'. When is this? Well, if you wire an event handler in class A to an event of class B and A gets out of scope. You might think: "A is out of scope, it gets cleaned up.", but that's not true. The event of B which is wired to a method (via a delegate) of A is in fact still referencing A and therefor A will be kept in memory via the delegate added to the event of B.

So, as a rule of thumb, always de-wire event handlers when you want the object which contains the event handler to go out of scope (and the object which holds the event does not) and to get cleaned up by the GC. Most of the time you'll need this in windows forms, where you'll wire an event handler in the form class to a given object which will eventually fire events which will have some effect on the form's contents. When you close the form and the object wired to the event handlers stays alive in another part of the application, the form is still in memory and will not be garbage collected due to the dangling event handler delegate. Add event handler cleanup code to the form closing event handlers to clean this up.

Of course a form itself doesn't have to do this with event handlers wired to events of its own controls. The reason why is left as an exercise to the reader ;) :)

Read: Collecting event handler / delegate garbage

Topic: E&C and Thinking Previous Topic   Next Topic Topic: A's Trade for Big Bat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use