<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dispose Pattern and Object Lifetime [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx</link><description>The Dispose pattern is the way to think of object lifetime in the .NET Framework. Admittedly, it can be a little subtle. A customer asked a question on our MSDN documentation for implementing the Dispose pattern . I’ll get to this question, but let’s</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Web 2.0 - Social Media - Internet News - Blogging &amp;raquo; Dispose</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5784864</link><pubDate>Tue, 30 Oct 2007 22:09:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5784864</guid><dc:creator>Web 2.0 - Social Media - Internet News - Blogging » Dispose</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://hyiplive.org/dispose"&gt;http://hyiplive.org/dispose&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Dispose Pattern and Object Lifetime</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5787876</link><pubDate>Wed, 31 Oct 2007 02:43:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5787876</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Dispose Pattern and Object Lifetime [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5806705</link><pubDate>Thu, 01 Nov 2007 02:27:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5806705</guid><dc:creator>Curt Nichols</dc:creator><description>&lt;p&gt;re Environment.HasShutdownStarted and finalizers: What prevents a race condition where shutdown begins *after* you check Environment.HasShutdownStarted?&lt;/p&gt;
</description></item><item><title>Izindaba #16</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5821071</link><pubDate>Fri, 02 Nov 2007 00:33:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5821071</guid><dc:creator>From the software development trenches</dc:creator><description>&lt;p&gt;Time for another weekly round-up of developer news that focuses on .NET, agile and general development&lt;/p&gt;
</description></item><item><title>Izindaba #16 Reloaded</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5821211</link><pubDate>Fri, 02 Nov 2007 00:42:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5821211</guid><dc:creator>From the software development trenches</dc:creator><description>&lt;p&gt;Time for another weekly round-up of developer news that focuses on .NET, agile and general development&lt;/p&gt;
</description></item><item><title>Yet another good explanation on the pitfalls of Finalization</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5834086</link><pubDate>Fri, 02 Nov 2007 14:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5834086</guid><dc:creator>Bill Blogs in C#</dc:creator><description>&lt;p&gt;Brian Grunkmeyer has written a rather deep article for the BCL weblog that discusses many of the pitfalls&lt;/p&gt;
</description></item><item><title>re: Dispose Pattern and Object Lifetime [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#5989222</link><pubDate>Thu, 08 Nov 2007 18:42:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5989222</guid><dc:creator>Aaron</dc:creator><description>&lt;p&gt;Thank You Chris Sells for yelling profusely loud to make sure this feature go in!&lt;/p&gt;
</description></item><item><title>Re: HasShutdownStarted &amp; AD unloads</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#6157608</link><pubDate>Tue, 13 Nov 2007 04:04:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6157608</guid><dc:creator>Brian Grunkemeyer</dc:creator><description>&lt;p&gt;Curt Nichols asked a great question:&lt;/p&gt;
&lt;p&gt;&amp;gt; What prevents a race condition where shutdown begins *after* you &lt;/p&gt;
&lt;p&gt;&amp;gt; check Environment.HasShutdownStarted?&lt;/p&gt;
&lt;p&gt;The CLR's appdomain unload &amp;amp; shutdown techniques are surprisingly hard to get right. &amp;nbsp;I think Chris Brumme identified roughly nine steps for shutting down the CLR in a sane manner. &amp;nbsp;The key part is that during an AD unload, we do multiple rounds of collecting &amp;amp; finalizing objects. &amp;nbsp;We first collect all normal objects, run their finalizers, then we collect all remaining objects &amp;amp; finalize them. &amp;nbsp;These are two different, synchronized steps during AD unloads. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Dispose Pattern and Object Lifetime [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#6285747</link><pubDate>Fri, 16 Nov 2007 06:06:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6285747</guid><dc:creator>dalevine</dc:creator><description>&lt;p&gt;Hello Brian,&lt;/p&gt;
&lt;p&gt;Thanks for such a deep post. I've a question or two on the UsesADisposableResource class. &lt;/p&gt;
&lt;p&gt;I see value in maintaining the _disposed flag - I can envision reasons to test for that. &lt;/p&gt;
&lt;p&gt;My first question regards this part of the Dispose pattern...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; if (disposing) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (_resource != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _resource.Dispose();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;Is the test of the 'disposing' flag prior to invoking _resource.Dispose() the best way of handling this? &lt;/p&gt;
&lt;p&gt;It &amp;quot;feels&amp;quot; like this is avoiding calling a Dispose method because we are not sure it is implemented correctly on the other side, rather than relying on it maintaining its own state to ensure that it will do the right thing regardless of how it is invoked, even if it is invoked after it has been finalized. If it was always invoked, and _resource.Dispose was implemented correctly, then we can ensure that cleanup will always occur, regardless of the code path it took to get there. &lt;/p&gt;
&lt;p&gt;This requires that all Dispose methods be written to handle being invoked more than once, but this can happen anyway and objects need to safeguard themselves against this.&lt;/p&gt;
&lt;p&gt;Second, there's the issue of handling exceptions. Regardless of when it is invoked, _resource.Dispose() can throw an exception, and the code as shown will not set any of the state variables to indicate that its Dispose method has already been invoked. And as you point out, when Thread aborts are considered an exception can occur anywhere.&lt;/p&gt;
&lt;p&gt;Assuming the call flow is through Dispose() --&amp;gt; Dispose(disposing), then if an exception occurs during Dispose(true), then GC.SuppressFinalize will not get called in the Dispose() method. If there is no derived class's Finalizer then it does not matter (other than some cleanup may not occur), but if a derived class does implement a Finalizer, then the Dispose method will get called again, and could throw another exception. &lt;/p&gt;
&lt;p&gt;A different approach could be to wrap the call to _resource.Dispose() in a try-finally, and set _resource=null; in there. Another is to wrap the entire Dispose method in a try-finally and set all the state variables in the finally clause. And yet another is to set each state field to a null value before invoking the _resource.Dispose, like this...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; if (disposing) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; 	 &amp;nbsp; “IDisposable” tmp = _resource;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; 	 &amp;nbsp; _resource = null;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (tmp != null) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tmp.Dispose();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;Even if this throws an exception the _resource field has already been set to null, so if the Dispose method is called again it will not attempt to call into the reource's Dispose method the second time. &lt;/p&gt;
&lt;p&gt;Third, the code has a comment about possibly using a lock around the Dispose logic. This is potentially risky during process exit (Environment.HasShutdownStarted). Chris Brumme ponted out that if the CLR is unloading then all threads other than the finalizer thread (and some internal threads) have been suspended and will never resume. If the Dispose method is waiting on a lock owned by one of those suspended threads then it will deadlock. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;If synchronization is needed in a Dispose method I would do so only if the disposing flag is true (not called from a finalizer) and the system is not shutting down, and then locking only on a private object, so other classes can't also take a lock on it.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;dal&lt;/p&gt;
</description></item><item><title>re: Dispose Pattern and Object Lifetime [Brian Grunkemeyer]</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#6457781</link><pubDate>Wed, 21 Nov 2007 20:00:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6457781</guid><dc:creator>Marco</dc:creator><description>&lt;p&gt;Your said:&lt;/p&gt;
&lt;p&gt;&amp;quot;...there’s no appdomain unload event&amp;quot;&lt;/p&gt;
&lt;p&gt;System.AppDomain class has:&lt;/p&gt;
&lt;p&gt;public event EventHandler DomainUnload&lt;/p&gt;
&lt;p&gt;Could you please clarify?&lt;/p&gt;
</description></item><item><title>Object Lifetime and Dispose Pattern</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#6675500</link><pubDate>Thu, 06 Dec 2007 09:20:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6675500</guid><dc:creator>Blog of Jitu</dc:creator><description>&lt;p&gt;Found a good article on Object Lifetime and Dispose Pattern .&lt;/p&gt;
</description></item><item><title>Object Lifetime and Dispose Pattern</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#6675664</link><pubDate>Thu, 06 Dec 2007 09:40:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6675664</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;Found a good article on Object Lifetime and Dispose Pattern .&lt;/p&gt;
</description></item><item><title>Difference between Disposing and Finalizing (referral)</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#8332557</link><pubDate>Sun, 23 Mar 2008 22:52:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8332557</guid><dc:creator>Kim Hamilton</dc:creator><description>&lt;p&gt;I've gotten some pings on the difference between Disposing and Finalizing, which I didn't discuss in&lt;/p&gt;
</description></item><item><title>Lipitor complications.</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#8705205</link><pubDate>Tue, 08 Jul 2008 03:20:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8705205</guid><dc:creator>Lipitor.</dc:creator><description>&lt;p&gt;Muscle loss due to lipitor. Lipitor and its bad effects. Lipitor vytorin heart attack. Lipitor side effects. Lipitor.&lt;/p&gt;
</description></item><item><title>Cleaning up after yourself (Dispose(void) doesn't get called for you)</title><link>http://blogs.msdn.com/bclteam/archive/2007/10/30/dispose-pattern-and-object-lifetime-brian-grunkemeyer.aspx#9116983</link><pubDate>Tue, 18 Nov 2008 09:06:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9116983</guid><dc:creator>Kim Hamilton</dc:creator><description>&lt;p&gt;Despite my large and growing number of Dispose-related blogs, I really don't like writing about Dispose.&lt;/p&gt;
</description></item></channel></rss>