diff --git a/src/SlatedGameToolkit.Framework/Utilities/Collections/Pooling/ObjectPool.cs b/src/SlatedGameToolkit.Framework/Utilities/Collections/Pooling/ObjectPool.cs index 60e3dc4..412d701 100644 --- a/src/SlatedGameToolkit.Framework/Utilities/Collections/Pooling/ObjectPool.cs +++ b/src/SlatedGameToolkit.Framework/Utilities/Collections/Pooling/ObjectPool.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using SlatedGameToolkit.Framework.Exceptions; namespace SlatedGameToolkit.Framework.Utilities.Collections.Pooling { @@ -23,6 +24,7 @@ namespace SlatedGameToolkit.Framework.Utilities.Collections.Pooling } public void CreateAmount(int amount) { + if (Size >= 0 && Count + amount >= Size) throw new FrameworkUsageException(string.Format("Object pool surpassed set size of {0}", Size)); for (int i = 0; i < amount; i++) { pool.Add(creator()); } @@ -30,7 +32,7 @@ namespace SlatedGameToolkit.Framework.Utilities.Collections.Pooling public Poolable Retrieve() { if (pool.Count == 0) { - pool.Add(creator()); + CreateAmount(1); } Poolable result; pool.TryTake(out result); @@ -38,6 +40,7 @@ namespace SlatedGameToolkit.Framework.Utilities.Collections.Pooling } public void Release(Poolable poolable) { + if (Size >= 0 && Count + 1 >= Size) throw new FrameworkUsageException(string.Format("Object pool surpassed set size of {0}", Size)); poolable.Reset(); pool.Add(poolable); }