﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Garbage collection and manual intervention</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Tue, 07 Apr 2026 15:39:09 GMT</lastBuildDate><ttl>20</ttl><item><title>Garbage collection and manual intervention</title><link>http://forum.strataframe.net/FindPost9667.aspx</link><description>G'day&lt;P&gt;I was just reading through the forum looking for some information on broken rules (found it!) when I came across the topic&lt;/P&gt;&lt;P&gt;&lt;A href="http://forum.strataframe.net/Topic4577-6-1.aspx"&gt;http://forum.strataframe.net/Topic4577-6-1.aspx&lt;/A&gt;?&lt;/P&gt;&lt;P&gt;In this Trent states&lt;/P&gt;&lt;P&gt;[quote]... the number one killer of many .NET developers is not cleaning up after the code and expecting the garbage collector to do the work.&amp;nbsp; The other thing, that goes hand in hand here, are event handlers.&amp;nbsp; If you ever gain a handler reference to any object, not just a BO, it will stay there until you explicitly remove the handler.&amp;nbsp; ... you will definitely want to Dispose your manually created objects when you are through with them...whether it be a BO or some other type of object. [/quote]&lt;/P&gt;&lt;P&gt;Previously I hadn't given much thought to garbage collection and disposing of objects, but I will certainly try to now.&lt;/P&gt;&lt;P&gt;I didn't quite follow what was said next about event handlers. &lt;/P&gt;&lt;P&gt;My understanding is that if in a form I have something like:&lt;/P&gt;&lt;P&gt;[codesnippet]&lt;FONT size=2&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Private&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Sub&lt;/FONT&gt;&lt;FONT size=2&gt; btnRefresh_Click(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ByVal&lt;/FONT&gt;&lt;FONT size=2&gt; sender &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;As&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Object&lt;/FONT&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;ByVal&lt;/FONT&gt;&lt;FONT size=2&gt; e &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;As&lt;/FONT&gt;&lt;FONT size=2&gt; System.EventArgs) &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Handles&lt;/FONT&gt;&lt;FONT size=2&gt; btnRefresh.Click&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Me&lt;/FONT&gt;&lt;FONT size=2&gt;.PopulateGrid1()&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;End&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Sub&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;[/codesnippet]&lt;/P&gt;&lt;P&gt;This will be garbage collected&amp;nbsp; when the form&amp;nbsp;(i.e. the class handling the event) exits.&lt;/P&gt;&lt;P&gt;Is this correct?&lt;/P&gt;&lt;P&gt;Could someone provide an example of an event handler that wouldn't be disposed, say when a form exits, and how and where it should be disposed if I did want to get rid of it when the form exits?&lt;/P&gt;&lt;P&gt;Peter</description><pubDate>Thu, 21 Jun 2007 09:38:35 GMT</pubDate><dc:creator>Peter Denton</dc:creator></item><item><title>RE: Garbage collection and manual intervention</title><link>http://forum.strataframe.net/FindPost9700.aspx</link><description>Excellent :)</description><pubDate>Thu, 21 Jun 2007 09:38:35 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Garbage collection and manual intervention</title><link>http://forum.strataframe.net/FindPost9687.aspx</link><description>Thanks Ben&lt;/P&gt;&lt;P&gt;&amp;nbsp;I think I've got it now.&lt;/P&gt;&lt;P&gt;Peter</description><pubDate>Wed, 20 Jun 2007 20:09:06 GMT</pubDate><dc:creator>Peter Denton</dc:creator></item><item><title>RE: Garbage collection and manual intervention</title><link>http://forum.strataframe.net/FindPost9668.aspx</link><description>In VB, if you use the Handles keyword, you don't have to worry about cleaning it up.&amp;nbsp; In fact, there's really not a keyword to remove an implicitly created handler like that...&lt;/P&gt;&lt;P&gt;However, if you ever use the "AddHandler obj.Event, AddressOf MethodHandler" to add an event that you didn't define WithEvents, you should remember to call the "RemoveHandler obj.Event, AddressOf MethodHandler" on it.</description><pubDate>Wed, 20 Jun 2007 09:07:03 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item></channel></rss>