<?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>Chris Lyon's WebLog : Dispose</title><link>http://blogs.msdn.com/clyon/archive/tags/Dispose/default.aspx</link><description>Tags: Dispose</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>New Dispose Guidelines</title><link>http://blogs.msdn.com/clyon/archive/2005/04/14/408243.aspx</link><pubDate>Thu, 14 Apr 2005 23:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:408243</guid><dc:creator>clyon</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/clyon/comments/408243.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=408243</wfw:commentRss><description>&lt;P&gt;Joe Duffy has posted the revised &lt;A href="http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae"&gt;Dispose, Finalization, and Resource Management&lt;/STRONG&gt; Design Guideline&lt;/A&gt;. It's a great (and long) read. Check it out! &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=408243" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/Dispose/default.aspx">Dispose</category></item><item><title>The Truth About GCHandles</title><link>http://blogs.msdn.com/clyon/archive/2005/03/18/398795.aspx</link><pubDate>Fri, 18 Mar 2005 20:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:398795</guid><dc:creator>clyon</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/clyon/comments/398795.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=398795</wfw:commentRss><description>&lt;p&gt;I've heard several people asking why GCHandle doesn't implement IDisposable, considering it wraps an unmanaged resource (a handle) and needs to be explicitly freed (using GCHandle.Free()). Before I explain the reason, I want to give a little background on GCHandles and their dangers. &lt;/p&gt; &lt;p&gt;What's a GCHandle? &lt;/p&gt; &lt;p&gt;A GCHandle is a struct that contains a handle to an object. It's mainly used for holding onto a managed object that gets passed to the unmanaged world to prevent the GC from collecting the object. You can also create a Pinned GCHandle to a managed object and retrieve the object's address in memory. &lt;/p&gt; &lt;p&gt;How are GCHandles dangerous? &lt;/p&gt; &lt;p&gt;When you create a new GCHandle, a new entry in the AppDomain's handle table is created. This entry is kept until the handle is freed (via GCHandle.Free()) or the AppDomain is unloaded. &lt;/p&gt; &lt;p&gt;Things get tricky if you were to make a copy of a GCHandle: &lt;/p&gt; &lt;p class="code"&gt;Object obj = new Object();&lt;br /&gt;GCHandle gch = GCHandle.Alloc(obj, GCHandleType.Normal);&lt;br /&gt;GCHandle gch2 = gch;&lt;br /&gt;&lt;/p&gt; &lt;p&gt;Since GCHandle is value type, gch2 has its own copy of the handle. You now have two handles that point to the same entry in the handle table. Unfortunately, since gch2 is a copy of –not a reference to– gch, anything that happens to gch doesn't happen to gch2. For example, calling gch.Free() will delete the entry from the handle table, but not update gch2, so gch2.IsAllocated will return true, but gch2.Target will be null. The same problem arises with casting to and from IntPtrs, and when GCHandles get boxed. Unlike double-freeing a single GCHandle, freeing the copy will NOT throw an InvalidOperationException. You have to be very careful not to double-Free your handles since this can corrupt the handle table. &lt;/p&gt; &lt;p&gt;Why don't GCHandles implement IDisposable? &lt;/p&gt; &lt;p&gt;One of the main purposes of IDisposable to avoid the use of finalizers. This is because finalizers are not run deterministically, and result in promoting a finalizable object a generation, effectively keeping in memory longer. Since GCHandle is a value type, it has no finalizer, and is not collected by the GC, so these problems are eliminated. Another other main use of IDisposable is to clean up unmanaged resources as soon as you are done with them. With a GCHandle, the resource is the handle which is cleaned up by calling GCHandle.Free(). If Free isn't called, the handle gets cleaned up when the appdomain is unloaded. &lt;/p&gt; &lt;p&gt;One of the side effects of having a struct implement IDisposable, is that users may be tempted to cast their GCHandles as IDisposable, which boxes the GCHandle into an IDisposable object on the heap, and the two GCHandles get out-of-sync. The same problem arises with the putting a disposable struct into a using block: &lt;/p&gt; &lt;p class="code"&gt;struct Test : IDisposable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public bool disposed; // initialized to false&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Dispose()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disposed = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void Foo()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Test t = new Test();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;using (t)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// do stuff&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (!t.disposed)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.Dispose();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt; &lt;p&gt;t.disposed will return false, since it was a copy of t whose Dispose method was called. If t were a GCHandle, then the handle would be removed from the appdomain's handle table, and calling Free after the using would result in a double Free, even though IsAllocated would return true! &lt;/p&gt; &lt;p&gt;Remember, GCHandles are advanced structures, and one should be very careful to ensure they are cleaned up properly. Unfortunately, IDisposable makes it easy to get this wrong, so the BCL designers erred on the side of caution, and gave GCHandle a Free() method to use. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=398795" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/Dispose/default.aspx">Dispose</category><category domain="http://blogs.msdn.com/clyon/archive/tags/GCHandles/default.aspx">GCHandles</category></item><item><title>Dispose Dos and Don'ts</title><link>http://blogs.msdn.com/clyon/archive/2004/09/23/233464.aspx</link><pubDate>Thu, 23 Sep 2004 16:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:233464</guid><dc:creator>clyon</dc:creator><slash:comments>18</slash:comments><comments>http://blogs.msdn.com/clyon/comments/233464.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=233464</wfw:commentRss><description>&lt;p&gt;Due to the positive response on my previous entry on Dispose, I thought I'd write another, this time on what one should and shouldn't do with a Dispose method.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Dispose should contain code to release any unmanaged resources associated with the object's instance. For example, any handles should be released, database connections closed, etc. This simulates deterministic finalization by letting the developer do the resource cleanup when he/she is done with the object. You may want to use an IsDisposed bool and check it inside Dispose. &lt;li&gt;Dispose should be safely callable multiple times. In my example above, if some user of your class called Dispose twice, we would crash with an unhandled InvalidOperationException trying to “double free” the GCHandle. &lt;li&gt;If you inherit from an IDisposable object, call the base class' Dispose method. Unlike finalizers, which the GC calls, Dispose is only called by user code (sometimes automatically in C#, as I covered previously). This means any unmanaged resources held by your object's parent, must also be disposed of. &lt;li&gt;Suppress finalization in Dispose. There has been some debate about whether GC.SuppressFinalization(this) should go before or after the cleanup code. Some argue after, in case something goes wrong before you've finished cleaning up. Others argue before, otherwise you may open up a race condition between your finalizer and your Dispose method. You should consider both strategies depending on your object's implementation. &lt;li&gt;Don't throw exceptions in Dispose. Nothing should go wrong with your object calling Dispose. Mainly for reasons stated above. &lt;li&gt;Throw ObjectDisposedException if a caller tries to use a released resource. You may be in a situation where you've disposed of your object because you no longer need the unmanaged resource, but you still need to have the object hanging around. If this is the case, any methods that would normally access the resource should throw an ObjectDisposedException, while other (possibly useful) methods should not. &lt;li&gt;Don't call Dispose from one thread, while still using the object on another. Try to make the thread that created the object be the one that disposes of it. This is more general good multithreaded programming advice, then Dispose specific. &lt;li&gt;Whenever possible, follow the Dispose Pattern. That link, one more time: &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp.&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If you have any other Dispose Do's and Don'ts, please post them in the comments.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=233464" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/Dispose/default.aspx">Dispose</category></item><item><title>Demystifying Dispose</title><link>http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx</link><pubDate>Tue, 21 Sep 2004 16:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:232445</guid><dc:creator>clyon</dc:creator><slash:comments>36</slash:comments><comments>http://blogs.msdn.com/clyon/comments/232445.aspx</comments><wfw:commentRss>http://blogs.msdn.com/clyon/commentrss.aspx?PostID=232445</wfw:commentRss><description>&lt;p&gt;One commonly misunderstood feature is the relationship between disposable objects (Objects that implement the IDisposable interface) and the GC.&amp;nbsp; I know there are a lot of online resources about &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp"&gt;patterns and best practices&lt;/a&gt; but there is still a lot of confusion.&amp;nbsp; I remember going to a Dev Lab to answer some questions by Whidbey early adopters, and being asked at what point in an object’s lifetime does the GC call Dispose.&amp;nbsp; The answer: Never.&lt;/p&gt; &lt;p&gt;Dispose is just a method, like any other.&amp;nbsp; Its purpose is allow deterministic resource cleanup, much like C++’s destructors, but without freeing the object’s memory.&amp;nbsp; Like a C++ destructor, the developer puts whatever cleanup code is necessary to be performed when the object’s lifetime is up.&amp;nbsp; Unlike a destructor, the object lives on after being "disposed of".&amp;nbsp; This means an object may be in an invalid state after being disposed, and developers should consider using ObjectDisposedExceptions when methods in a disposed object are called.&lt;/p&gt; &lt;p&gt;What happens when you call Dispose()&lt;/p&gt; &lt;p&gt;Any cleanup your object need done in a timely fashion should be done in Dispose.&amp;nbsp; Things like closing database connections, closing files, releasing bitmaps, etc.&amp;nbsp; Unmanaged resources in particular should be released in Dispose.&amp;nbsp; This is because the GC has no knowledge of anything not allocated on the managed heap.&amp;nbsp; For example, a Bitmap object that encapsulates a 2MB image file reports only its managed size (the size of the managed object) to the GC.&amp;nbsp; The GC knows nothing about the 2MB unmanaged image, thus will not collect it from memory.&amp;nbsp; By calling Dispose, you tell the Bitmap object that you are finished with it, and it will release the image itself.&lt;/p&gt; &lt;p&gt;What doesn’t happen when you call Dispose()&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Calling Dispose does not prioritize the object for garbage collection. It simply unloads the object’s (unmanaged) resources from memory. &lt;li&gt;Calling Dispose does not deallocate the object from memory.&amp;nbsp; Only the GC does that when it performs a collection of the generation in which the object resides. &lt;li&gt;The CLR does not insert or run any code not in Dispose.&amp;nbsp; The behaviour of Dispose is defined by the developer. &lt;li&gt;Dispose must be called explicitly by the application.&amp;nbsp; It is never called by the runtime.&amp;nbsp; The only exceptions&amp;nbsp;are when using C#’s using statement (see ShawnFa’s &lt;A href="http://blogs.msdn.com/shawnfa/archive/2004/03/29/101466.aspx"&gt;article&lt;/a&gt; for a good explanation of what exactly goes on), and the foreach keyword in C# will result in Dispose being called on the Enumerator &lt;li&gt;Dispose is NOT threadsafe.&amp;nbsp; This means two threads can call Dispose on the same object at the same time.&amp;nbsp; Like for any other synchronization-sensitive method, take steps to make sure this doesn’t happen.&amp;nbsp; I’ll give an example of when this might happen in the next section.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;What about the finalizer?&lt;/p&gt; &lt;p&gt;There are a few reasons why not to rely on the finalizer to clean up resources that I’ll cover in a future blog entry on finalizers.&amp;nbsp; The main reasons you should be concerned with are performance and determinism.&amp;nbsp; Finalizers are expensive to run, and there’s no order (or even guarantee) that they will be run (for example, the finalizer thread may time out, or be killed on AppDomain unload).&lt;/p&gt; &lt;p&gt;That being said, the finalizer should be your last chance to clean up resources, in case someone using your class forgets to dispose of it when done.&amp;nbsp; A call to Dispose inside the finalizer reduces duplication of code.&amp;nbsp; However, you want to make sure the resources don’t get released twice.&amp;nbsp;&amp;nbsp; In your Dispose method, you want to make sure you call GC.SuppressFinalize(this) after your clean up code.&amp;nbsp; If you suppress the finalizer before the clean up, you’re limiting your ability to recover from failures during the cleanup. &lt;/p&gt; &lt;p&gt;Some of you may have noticed a race condition in my description above.&amp;nbsp; Consider a situation where the call to Dispose is the last time your object is being referenced.&amp;nbsp; As soon as Dispose is entered, the object is eligible for collection by the GC.&amp;nbsp; Before that happens, the object’s finalizer gets called, which calls Dispose.&amp;nbsp; You now have two threads inside Dispose, possibly double-freeing resources (this could be bad, as in the case of GCHandles).&amp;nbsp; Make sure you follow the pattern I linked to above and use a disposing flag to avoid problems like this.&lt;/p&gt; &lt;p&gt;For more information about an object being collected while one of its methods are being run, check out these (very complete) blog posts by Chris Brumme: (&lt;A href="http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx"&gt;Finalization&lt;/a&gt; and &lt;A href="http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx"&gt;Lifetime, GC.KeepAlive, handle recycling&lt;/a&gt;).&lt;/p&gt; &lt;p&gt;If your .NET application is using unmanaged resources, make sure to implement the Dispose pattern, secure in the knowledge that nothing magical is going on.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;EDIT:&lt;/strong&gt; Added point about Dispose and foreach. Thanks Steve!&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt;&amp;nbsp; Minor corrections.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=232445" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/clyon/archive/tags/Dispose/default.aspx">Dispose</category></item></channel></rss>