@@ -13,13 +13,15 @@ public sealed class SwiftObjectPool<T> : IDisposable, ISwiftObjectPool<T> where
1313 {
1414 #region Fields
1515
16- private readonly ConcurrentBag < T > _pool ;
16+ private readonly ConcurrentStack < T > _pool ;
1717 private readonly Func < T > _createFunc ;
1818 private readonly Action < T > _actionOnGet ;
1919 private readonly Action < T > _actionOnRelease ;
2020 private readonly Action < T > _actionOnDestroy ;
2121 private readonly int _maxSize ;
2222
23+ private volatile bool _disposed ;
24+
2325 #endregion
2426
2527 #region Constructor
@@ -44,7 +46,7 @@ public SwiftObjectPool(
4446 if ( createFunc == null ) ThrowHelper . ThrowArgumentNullException ( nameof ( createFunc ) ) ;
4547 if ( maxSize <= 0 ) ThrowHelper . ThrowArgumentException ( $ "{ nameof ( maxSize ) } must be greater than 0") ;
4648
47- _pool = new ConcurrentBag < T > ( ) ;
49+ _pool = new ConcurrentStack < T > ( ) ;
4850 _createFunc = createFunc ;
4951 _actionOnGet = actionOnGet ;
5052 _actionOnRelease = actionOnRelease ;
@@ -83,7 +85,7 @@ public SwiftObjectPool(
8385 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
8486 public T Rent ( )
8587 {
86- if ( _pool . TryTake ( out var obj ) )
88+ if ( _pool . TryPop ( out var obj ) )
8789 {
8890 _actionOnGet ? . Invoke ( obj ) ;
8991 return obj ;
@@ -118,7 +120,7 @@ public void Release(T element)
118120 _actionOnRelease ? . Invoke ( element ) ;
119121
120122 if ( _pool . Count < _maxSize )
121- _pool . Add ( element ) ;
123+ _pool . Push ( element ) ;
122124 else
123125 {
124126 _actionOnDestroy ? . Invoke ( element ) ;
@@ -131,7 +133,10 @@ public void Release(T element)
131133 /// </summary>
132134 public void Clear ( )
133135 {
134- while ( _pool . TryTake ( out var obj ) )
136+ if ( _disposed )
137+ return ;
138+
139+ while ( _pool . TryPop ( out var obj ) )
135140 _actionOnDestroy ? . Invoke ( obj ) ;
136141
137142 CountAll = 0 ;
@@ -141,8 +146,27 @@ public void Clear()
141146
142147 #region IDisposable Implementation
143148
144- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
145- public void Dispose ( ) => Clear ( ) ;
149+ /// <summary>
150+ /// Releases all resources used by the SwiftArrayPool.
151+ /// It is important to call Dispose() to release pooled arrays, preventing potential memory leaks.
152+ /// </summary>
153+ public void Dispose ( )
154+ {
155+ OnDispose ( ) ;
156+ GC . SuppressFinalize ( this ) ; // Avoids calling the finalizer if already disposed.
157+ }
158+
159+ private void OnDispose ( )
160+ {
161+ if ( _disposed )
162+ return ;
163+
164+ _disposed = true ;
165+
166+ _pool . Clear ( ) ;
167+ }
168+
169+ ~ SwiftObjectPool ( ) => OnDispose ( ) ; // Called by GC if Dispose() wasn't called explicitly.
146170
147171 #endregion
148172 }
0 commit comments