<?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>‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx</link><description>This isn’t fully game related, but I see so many people posting code on forums that looks like this: Stream stream = storageContainer.CreateFile( &amp;quot;MyFile.txt&amp;quot; );
 StreamWriter writer = new StreamWriter (stream);
writer.Write( &amp;quot;Stuff&amp;quot;</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10114131</link><pubDate>Tue, 11 Jan 2011 12:32:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10114131</guid><dc:creator>Alex Skorkin</dc:creator><description>&lt;p&gt;Peter Gfader,&lt;/p&gt;
&lt;p&gt;CodeRush Pro has &amp;quot;code issues&amp;quot; feature that highlights undisposed locals and has a fix to automatically apply a using statement on code in question.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10114131" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10112312</link><pubDate>Thu, 06 Jan 2011 02:32:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10112312</guid><dc:creator>Peter Gfader</dc:creator><description>&lt;p&gt;Is there a tool that can warn us when we new up instances of IDispsable without using statement?&lt;/p&gt;
&lt;p&gt;E.g. Reshaper, JustCode, CodeRush, Code Analysis...&lt;/p&gt;
&lt;p&gt;It would be nice to tell us if we create of a &amp;quot;Type&amp;quot; that implements IDisposable and that is not used with a using statement...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10112312" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10111610</link><pubDate>Tue, 04 Jan 2011 19:21:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10111610</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;It&amp;#39;s not just Mono or third-party libraries which have problems implementing IDisposable - try disposing of a ReaderWriteLockSlim twice and see what happens.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="https://connect.microsoft.com/VisualStudio/feedback/details/531334/readerwriterlockslim-does-not-implement-idisposable-properly"&gt;connect.microsoft.com/.../readerwriterlockslim-does-not-implement-idisposable-properly&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cygnus: If you want a language with determinate destructors like C++/CLI, then why don&amp;#39;t you try C++/CLI? I hear it can handle this much better. :o)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10111610" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10111247</link><pubDate>Mon, 03 Jan 2011 21:27:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10111247</guid><dc:creator>Cygnus</dc:creator><description>&lt;p&gt;This is why C# is still a toy - its stubborn refusal to have determinate destructors. &amp;nbsp;C++/CLI can handle this much better. &amp;nbsp;It should not be necessary to know if native resources need to be released. &amp;nbsp;Resources should be released when no longer necessary - period.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10111247" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10110751</link><pubDate>Sat, 01 Jan 2011 07:01:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10110751</guid><dc:creator>StarTrekRedneck</dc:creator><description>&lt;p&gt;Regarding the VB translation, for brevity we can combine them into one Using statement:&lt;/p&gt;
&lt;p&gt;Using stream As Stream = storageContainer.CreateFile(&amp;quot;MyFile.txt&amp;quot;), &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;writer As New StreamWriter(stream)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;writer.Write(&amp;quot;Stuff&amp;quot;)&lt;/p&gt;
&lt;p&gt;End Using&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10110751" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10110600</link><pubDate>Fri, 31 Dec 2010 12:36:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10110600</guid><dc:creator>Stark</dc:creator><description>&lt;p&gt;I agree that you should at least be careful about bad implementations and test it accordingly, then you could proceed to either fix the &amp;#39;bug&amp;#39; if you can or go for the try-finally alternative. You can never be sure unless it&amp;#39;s a .NET type that the 2nd object&amp;#39;s Dispose will take care of the 1st one.&lt;/p&gt;
&lt;p&gt;Also, just in case, especially if you can&amp;#39;t use the using because you need to return the object, you could implement the special &amp;#39;finalize&amp;#39; method so that it calls the Dispose if the object is not disposed already.&lt;/p&gt;
&lt;p&gt;In the end, nothing can replace a good testing strategy, though.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10110600" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10109979</link><pubDate>Wed, 29 Dec 2010 20:09:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10109979</guid><dc:creator>Werner van Deventer</dc:creator><description>&lt;p&gt;There is actually a problem with this and all the posts making use of nested &amp;#39;using&amp;#39; statements. If you use static code analysis you will be warned of nesting a &amp;#39;using&amp;#39; statement that uses the object created in the previous &amp;#39;using&amp;#39;. CA2202: &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/ms182334.aspx"&gt;msdn.microsoft.com/.../ms182334.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The issue is because the second object usually takes ownership of the first. When disposing the second/nested object (by going out of scope) the first object will get it&amp;#39;s dispose method called as well. When the first object goes out of scope the dispose method will be called again.&lt;/p&gt;
&lt;p&gt;This is not usually a problem when coding with built-in .NET types but generally isn&amp;#39;t good for business, as long as MS keeps to their own standard... from the rule page itself &amp;quot;A correctly implemented Dispose method can be called multiple times without throwing an exception. However, this is not guaranteed and to avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.&amp;quot;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been bitten by this issue in Mono and it will probably happen again when using third party libraries that don&amp;#39;t implement IDisposable correctly. Better safe than sorry!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10109979" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10109970</link><pubDate>Wed, 29 Dec 2010 19:53:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10109970</guid><dc:creator>Greg</dc:creator><description>&lt;p&gt;Thank you, this was very helpful. &amp;nbsp;I am now updating my current project to make use of this, but since I am doing this in VB, I thought I would add a translation.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Using Stream As Stream = storageContainer.CreateFile(&amp;quot;MyFile.txt&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Using writer As New StreamWriter(Stream)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;writer.Write(&amp;quot;Stuff&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Using&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10109970" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10109920</link><pubDate>Wed, 29 Dec 2010 17:10:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10109920</guid><dc:creator>Siderite</dc:creator><description>&lt;p&gt;I also love the pattern when you implement IDisposable on a token object so that the Dispose method would cancel an action or unregister an event. Something like var token=BindToEvent() and then, when you want to stop handling the event, token.Dispose().&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10109920" width="1" height="1"&gt;</description></item><item><title>re: ‘using’ IDisposable objects</title><link>http://blogs.msdn.com/b/nicgrave/archive/2010/12/28/using-idisposable-objects.aspx#10109881</link><pubDate>Wed, 29 Dec 2010 14:35:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10109881</guid><dc:creator>Parrotlover77</dc:creator><description>&lt;p&gt;MikeBMcL - I agree with the dissenters on that MSDN page and the author of this article: nested &amp;#39;usings&amp;#39; encourages good coding practice by keeping it simple, readable, and consistent. &amp;nbsp;CA2202 makes no sense. &amp;nbsp;If a dispose method is incorrectly implemented and multiple disposes causes a problem, then that&amp;#39;s a bug in the library and the library should be fixed. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you said, it&amp;#39;s very easy to test for, so unit testing the class&amp;#39;s dispose is a no-brainer. &amp;nbsp;The outer try-finally style is atrocious.&lt;/p&gt;
&lt;p&gt;BigFredd - braces are for teeth, curly braces are for coders. &amp;nbsp;;-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10109881" width="1" height="1"&gt;</description></item></channel></rss>