<?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>When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx</link><description>A recent internal email thread unearthed extreme differences of opinion about when Dispose(void) should be called on an IDisposable. This led to a long discussion and a realization that -- while it seems like we’ve said everything there is to say about</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>When to call Dispose | MS Tech News</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9047271</link><pubDate>Thu, 06 Nov 2008 09:45:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9047271</guid><dc:creator>When to call Dispose | MS Tech News</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://mstechnews.info/2008/11/when-to-call-dispose/"&gt;http://mstechnews.info/2008/11/when-to-call-dispose/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9049122</link><pubDate>Thu, 06 Nov 2008 17:24:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9049122</guid><dc:creator>Bradley Grainger</dc:creator><description>&lt;p&gt;Hi Kim,&lt;/p&gt;
&lt;p&gt;There's another Dispose-related special case: &amp;quot;IDisposable is not implemented correctly&amp;quot;.&lt;/p&gt;
&lt;p&gt;IDisposable on ClientBase&amp;lt;T&amp;gt; (from WCF) is implemented in a way that can sometimes cause it to throw (which violates the &amp;quot;Dispose should never throw&amp;quot; guideline for implementing the dispose pattern).&lt;/p&gt;
&lt;p&gt;The official guideline for WCF clients (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/aa355056.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa355056.aspx&lt;/a&gt;) now recommends that the &amp;quot;using&amp;quot; statement not be used (and thus Dispose not be called), and instead suggests a complicated series of multiple catch blocks.&lt;/p&gt;
&lt;p&gt;In a forum post (&lt;a rel="nofollow" target="_new" href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/b95b91c7-d498-446c-b38f-ef132989c154"&gt;http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/b95b91c7-d498-446c-b38f-ef132989c154&lt;/a&gt;), Brian McNamara wrote, &amp;quot;You can argue (and some of us did) that we should have removed [IDisposable] from those two classes [ServiceHost and ClientBase]&amp;quot;, indicating that perhaps the implementation of IDisposable on these WCF classes was a mistake.&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9049167</link><pubDate>Thu, 06 Nov 2008 17:33:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9049167</guid><dc:creator>Bradley Grainger</dc:creator><description>&lt;p&gt;As I'm sure you know, there is a cost to letting the finalizer clean up finalizable objects. (Chris Brumme goes into some reasons at &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx"&gt;http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx&lt;/a&gt;, which include promotion of objects, or possibly object graphs, to the next GC generation, and the current CLR only having one finalizer thread, which can limit scalability.)&lt;/p&gt;
&lt;p&gt;Thus, I would suggest adding another reason to call Dispose to category #2: When the cost of finalization is measurably affecting performance or memory use.&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9049718</link><pubDate>Thu, 06 Nov 2008 19:29:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9049718</guid><dc:creator>Ziv Caspi</dc:creator><description>&lt;p&gt;I would argue that it's WinForm that's at fault for not using the IDisposable contract correctly. They should simply have called their method something else, since not calling Dispose in this case is non-observable.&lt;/p&gt;
</description></item><item><title>Interesting Finds: 2008.11.01~2008.11.07</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9051060</link><pubDate>Fri, 07 Nov 2008 03:46:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9051060</guid><dc:creator>gOODiDEA.NET</dc:creator><description>&lt;p&gt;.NET What's New in the BCL in .NET 4.0 NetMon API &amp;amp;#8211; Capture, Parse and and Capture File Access&lt;/p&gt;
</description></item><item><title>Interesting Finds: 2008.11.01~2008.11.07</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9051066</link><pubDate>Fri, 07 Nov 2008 03:49:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9051066</guid><dc:creator>gOODiDEA</dc:creator><description>&lt;p&gt;.NETWhat'sNewintheBCLin.NET4.0NetMonAPI–Capture,ParseandandCaptureFileAccess(wi...&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9051776</link><pubDate>Fri, 07 Nov 2008 11:02:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9051776</guid><dc:creator>kimhamil</dc:creator><description>&lt;p&gt;Hi Bradley,&lt;/p&gt;
&lt;p&gt;It was nice to meet you at PDC last week. I wasn't aware of the WCF ServiceHost and ClientBase examples. I read the forum post and it's interesting that matching up Close and Dispose (after the fact) was involved in this behavior. It's indeed a common expectation that they do the same. As I talked about in another post, I believe we'd have fewer Dispose headaches if we'd never given the guidance of hiding Dispose in favor of domain-specific names. &lt;/p&gt;
&lt;p&gt;Finalization cost is an interesting point. It will be important to clarify that for category 2, we're just saying that calling Dispose isn't necessary from a correctness standpoint, as opposed to &amp;quot;don't call Dispose&amp;quot;. There are cases where users would want to.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Kim&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9051784</link><pubDate>Fri, 07 Nov 2008 11:05:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9051784</guid><dc:creator>kimhamil</dc:creator><description>&lt;p&gt;Hi Ziv,&lt;/p&gt;
&lt;p&gt;Unfortunately not calling Dispose _could_ be observable in extreme cases. Best example I can think of would be a targeted stress test. :) These are cases where it's hard to hit the limits, but we want to make sure apps that happen to do this are aware of the problem.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Kim&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9065885</link><pubDate>Thu, 13 Nov 2008 13:31:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9065885</guid><dc:creator>Mike Dimmick</dc:creator><description>&lt;p&gt;In WinForms, controls that are owned by another control (in that control's Controls collection, either by calling .Add or by setting the control's Parent property) are disposed automatically when that control is itself disposed. Top-level forms are disposed when the window is destroyed. This largely means that the programmer doesn't have to do anything. However, there is no Destroy method - you destroy a window by calling Dispose.&lt;/p&gt;
&lt;p&gt;In addition, any non-graphical components added to a form in Visual Studio's designer cause VS to generate code that adds them to a System.ComponentModel.Container named 'components', and an override of Dispose which disposes the container, which in turn disposes all of the contained components. This works for desktop apps but is BROKEN for smart device development (Compact Framework). I added a bug on this - Connect feedback ID 305534 - which has STILL had no reply after more than a year.&lt;/p&gt;
&lt;p&gt;On Compact Framework you don't have space for VM overflows, particularly on Windows Mobile 5.0 devices where there is often a tiny amount of VM actually available, due to the DLL loading model and vast number of eXecute In Place DLLs. Calling Dispose everywhere is absolutely required. Leaving it to Finalize is too late.&lt;/p&gt;
</description></item><item><title>re: When to call Dispose</title><link>http://blogs.msdn.com/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx#9108502</link><pubDate>Mon, 17 Nov 2008 12:24:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9108502</guid><dc:creator>Jérémie Chassaing</dc:creator><description>&lt;p&gt;A typical ambiguous case can be found with readers and writers (StreamReader, StreamWriter, BinaryReader, BinaryWriter...).&lt;/p&gt;
&lt;p&gt;They implement IDisposable just in case the have ownership on the underlying stream, but the documentation is not clear about it.&lt;/p&gt;
&lt;p&gt;When you need to keep the stream open (to use several differents writers, or when using a memory stream), you must use Flush and not Close (= Dispose).&lt;/p&gt;
&lt;p&gt;My opinion is that you should always call Flush, and then call Dispose only if you need to get rid of the underlying stream.&lt;/p&gt;
&lt;p&gt;A bit confusing...&lt;/p&gt;
</description></item></channel></rss>