/* Disposable.cs Copyright (c) 2007 Chris Peterson (cpeterso@cpeterso.com) codesnippet:B403AA8E-E3E4-4A74-B1DF-36C9BADF9648 For more information (than you really want to know) about IDisposable, see: http://www.bluebytesoftware.com/blog/2005/04/08/DGUpdateDisposeFinalizationAndResourceManagement.aspx Version History: 2011-03-25 = Test Mono. Add extra comments and robustification. 2008-01-18 = finalizer exceptions are ignored or fatal, so debug break instead 2008-01-09 = minor clean up 2007-10-23 = initial public release MIT LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define DEBUG #define DISPOSABLE_BREAK_ON_LEAK using System; using System.Diagnostics; namespace cp { public abstract class Disposable : IDisposable { [Conditional("DEBUG")] public static void DumpLeaks() { #if DEBUG // Run finalizers to find disposable objects that have been leaked (i.e. // not disposed). GC.Collect(); GC.WaitForPendingFinalizers(); // Try again. GC.Collect(); GC.WaitForPendingFinalizers(); #endif // DEBUG } protected bool Disposed { get { return _disposed; } } public void Dispose() { if (_disposed) return; #if DEBUG try #endif // DEBUG { Dispose(true); } #if DEBUG finally { if (!_disposed) ReportLeak("Did derived class forget to call base.Dispose(bool)?"); } #endif // DEBUG GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // This method should be called by the derived class's Dispose(bool). _disposed = true; } #if DEBUG ~Disposable() { // Gracefully handle situations in which your finalizer is invoked more than // once. This means you might need a way to detect whether finalization // has already occurred on a given instance. if (_disposed) return; // If a constructor throws an exception, the CLR will still call // the object's Finalize method. So, when your Finalize method is // called, the object's fields may not have all been initialized; // your Finalize method should be robust enough to handle this. // Once the object has been constructed, Finalize will only be called if // Dispose(bool) was not called! ReportLeak("Finalizer unexpectedly called. Did you forget to call Dispose()?"); } private void ReportLeak(string reason) { Console.WriteLine(String.Format("LEAKED {0}? {1}", ClassName, reason)); #if DISPOSABLE_BREAK_ON_LEAK if (Debugger.IsAttached) Debugger.Break(); #endif // DISPOSABLE_BREAK_ON_LEAK } private string ClassName { get { return string.Format("{0}", this); } } #endif // DEBUG private bool _disposed; } }