Make Counter<T> finalizable

Most certainly quite an odd use case.
This commit is contained in:
FICTURE7 2021-04-13 14:55:07 +04:00
parent 60e56f3efe
commit adb1dac60b

View file

@ -41,6 +41,39 @@ namespace ARMeilleure.Common
} }
} }
/// <summary>
/// Releases all resources used by the <see cref="Counter{T}"/> instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases all unmanaged and optionally managed resources used by the <see cref="Counter{T}"/> instance.
/// </summary>
/// <param name="disposing"><see langword="true"/> to dispose managed resources also; otherwise just unmanaged resouces</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
// The index into the EntryTable is essentially an unmanaged resource since we allocate and free the
// resource ourselves.
_countTable.Free(_index);
_disposed = true;
}
}
/// <summary>
/// Frees resources used by <see cref="Counter{T}"/> instance.
/// </summary>
~Counter()
{
Dispose(false);
}
/// <summary> /// <summary>
/// Tries to create a <see cref="Counter{T}"/> instance from the specified <see cref="EntryTable{T}"/> instance. /// Tries to create a <see cref="Counter{T}"/> instance from the specified <see cref="EntryTable{T}"/> instance.
/// </summary> /// </summary>
@ -77,18 +110,5 @@ namespace ARMeilleure.Common
return false; return false;
} }
/// <summary>
/// Releases all resources used by the <see cref="Counter{T}"/> instance.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
_countTable.Free(_index);
_disposed = true;
}
}
} }
} }