83 lines
2 KiB
C#
83 lines
2 KiB
C#
//#define DEBUG_NOTFINALIZABLE_ALLOC
|
|
|
|
using System;
|
|
|
|
namespace JD.Kernel.Classes
|
|
{
|
|
/// <summary>
|
|
/// An object that should always be disposed explictly.
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Default empty ctor.
|
|
/// </summary>
|
|
protected NotFinalizable()
|
|
: this(string.Empty)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ctor with an identifier.
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
protected NotFinalizable(string identifier)
|
|
{
|
|
#if DEBUG_NOTFINALIZABLE_ALLOC
|
|
m_identifier = identifier;
|
|
#endif
|
|
}
|
|
|
|
#if DEBUG_NOTFINALIZABLE_ALLOC
|
|
/// <summary>
|
|
/// Finalizer.
|
|
/// </summary>
|
|
~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
|
|
|
|
/// <summary>
|
|
/// Disposes the object.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
#if DEBUG_NOTFINALIZABLE_ALLOC
|
|
GC.SuppressFinalize(this);
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose.
|
|
/// </summary>
|
|
/// <param name="disposing"></param>
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|