Skip to content

Commit

Permalink
Merge pull request #535 from hadashiA/ku/clear-arrraypool
Browse files Browse the repository at this point in the history
Fix a memory leaking with CappedArrayPool
  • Loading branch information
hadashiA authored Jul 14, 2023
2 parents c194894 + e139ad5 commit 9d753be
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

1 comment on commit 9d753be

@vercel
Copy link

@vercel vercel bot commented on 9d753be Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.