Skip to content

Commit

Permalink
Fix a memory leaking with CappedArrayPool
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jul 14, 2023
1 parent c194894 commit e139ad5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void Return(T[] array)
var i = array.Length - 1;
lock (syncRoot)
{
Array.Clear(array, 0, array.Length);
if (tails[i] > 0)
tails[i] -= 1;
}
Expand Down
18 changes: 15 additions & 3 deletions VContainer/Assets/VContainer/Tests/CappedArrayPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ namespace VContainer.Tests
public class CappedArrayPoolTest
{
[Test]
public void RentWithExpandBucket()
public void Return_ClearElements()
{
var cappedArrayPool = new CappedArrayPool<object>(8);
var pool = new CappedArrayPool<object>(8);
var a = pool.Rent(4);
a[0] = new object();
a[3] = new object();
pool.Return(a);
Assert.That(a[0], Is.Null);
Assert.That(a[3], Is.Null);
}

[Test]
public void Rent_ExpandBucket()
{
var pool = new CappedArrayPool<object>(8);
Assert.DoesNotThrow(() =>
{
for (var i = 1; i < CappedArrayPool<object>.InitialBucketSize * 2; i++)
{
cappedArrayPool.Rent(1);
pool.Rent(1);
}
});
}
Expand Down

0 comments on commit e139ad5

Please sign in to comment.