From 6f4cb3c0ad70f721440e74f47ba50b307205c3c8 Mon Sep 17 00:00:00 2001 From: FICTURE7 Date: Sun, 11 Apr 2021 13:54:33 +0400 Subject: [PATCH] Make Counter disposable --- ARMeilleure/Common/Counter.cs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ARMeilleure/Common/Counter.cs b/ARMeilleure/Common/Counter.cs index f8c5d2732..8fd73f114 100644 --- a/ARMeilleure/Common/Counter.cs +++ b/ARMeilleure/Common/Counter.cs @@ -6,8 +6,9 @@ namespace ARMeilleure.Common /// Represents a numeric counter. /// /// Type of the counter - class Counter where T : unmanaged + class Counter : IDisposable where T : unmanaged { + private bool _disposed; private readonly int _index; private readonly EntryTable _countTable; @@ -26,7 +27,18 @@ namespace ARMeilleure.Common /// /// Gets a reference to the value of the counter. /// - public ref T Value => ref _countTable.GetValue(_index); + public ref T Value + { + get + { + if (_disposed) + { + throw new ObjectDisposedException(null); + } + + return ref _countTable.GetValue(_index); + } + } /// /// Tries to create a instance from the specified instance. @@ -64,5 +76,18 @@ namespace ARMeilleure.Common return false; } + + /// + /// Releases all resources used by the instance. + /// + public void Dispose() + { + if (!_disposed) + { + _countTable.Free(_index); + + _disposed = true; + } + } } } \ No newline at end of file