diff --git a/Assets/Mirror/Core/Tools/Pool.cs b/Assets/Mirror/Core/Tools/Pool.cs index e204575b6..f26d49b11 100644 --- a/Assets/Mirror/Core/Tools/Pool.cs +++ b/Assets/Mirror/Core/Tools/Pool.cs @@ -32,7 +32,15 @@ public Pool(Func objectGenerator, int initialCapacity) // return an element to the pool [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Return(T item) => objects.Push(item); + public void Return(T item) + { + // make sure we can't accidentally insert null values into the pool. + // debugging this would be hard since it would only show on get(). + if (item == null) + throw new ArgumentNullException(nameof(item)); + + objects.Push(item); + } // count to see how many objects are in the pool. useful for tests. public int Count => objects.Count; diff --git a/Assets/Mirror/Tests/Editor/Tools/PoolTests.cs b/Assets/Mirror/Tests/Editor/Tools/PoolTests.cs index cd1ad692c..670c36ce5 100644 --- a/Assets/Mirror/Tests/Editor/Tools/PoolTests.cs +++ b/Assets/Mirror/Tests/Editor/Tools/PoolTests.cs @@ -34,6 +34,15 @@ public void ReturnAndTake() Assert.That(pool.Get(), Is.EqualTo("returned")); } + [Test] + public void ReturnNull() + { + // make sure we can't accidentally insert null values into the pool. + // debugging this would be hard since it would only show on get(). + Assert.That(() => pool.Return(null), Throws.ArgumentNullException); + Assert.That(pool.Count, Is.EqualTo(0)); + } + [Test] public void Count() {