//#define DEBUG_NOTFINALIZABLE_ALLOC using System; namespace JD.Kernel.Classes { /// /// An object that should always be disposed explictly. /// public abstract class NotFinalizable : IDisposable { #region Variables #if DEBUG_NOTFINALIZABLE_ALLOC private readonly string m_allocationStackTrace = Environment.StackTrace; private readonly string m_identifier; #endif #endregion #region Constructor /// /// Default empty ctor. /// protected NotFinalizable() : this(string.Empty) { } /// /// Ctor with an identifier. /// /// protected NotFinalizable(string identifier) { #if DEBUG_NOTFINALIZABLE_ALLOC m_identifier = identifier; #endif } #if DEBUG_NOTFINALIZABLE_ALLOC /// /// Finalizer. /// ~NotFinalizable() { if (Debugger.IsAttached) { Debugger.Break(); } else { Throw.Assert(false, string.Format("This object ({0} : {1})should always be explicitly disposed. " + "Allocation stack trace: " + m_allocationStackTrace , m_identifier, GetType().FullName)); } Dispose(false); } #endif /// /// Disposes the object. /// public void Dispose() { Dispose(true); #if DEBUG_NOTFINALIZABLE_ALLOC GC.SuppressFinalize(this); #endif } /// /// Dispose. /// /// protected virtual void Dispose(bool disposing) { } #endregion } }