Skip to content

Commit

Permalink
fix(Pool): Return now checks for null entries to avoid nulls creeping…
Browse files Browse the repository at this point in the history
… into the pool
  • Loading branch information
miwarnec committed Apr 17, 2024
1 parent 7c3b0c1 commit c047b48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Assets/Mirror/Core/Tools/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ public Pool(Func<T> 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;
Expand Down
9 changes: 9 additions & 0 deletions Assets/Mirror/Tests/Editor/Tools/PoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit c047b48

Please sign in to comment.