Skip to content

Commit

Permalink
fix: Fix StackVector not copying elements over when resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Mar 13, 2024
1 parent bd73221 commit 1e451ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Uno.UI.Tests/Foundation/Given_StackVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,25 @@ public unsafe void ItemsInStackVector()
}
}
}

[TestMethod]
public void TestResizingShouldCopyOldElements()
{
var sut = new StackVector<int>(2);

for (int i = 0; i < 200; i++)
{
ref var item1 = ref sut.PushBack();
item1 = i;
}

sut.Count.Should().Be(200);
sut.Should().HaveCount(200);

for (int i = 0; i < 200; i++)
{
Assert.AreEqual(i, sut[i]);
}
}
}
}
7 changes: 6 additions & 1 deletion src/Uno.UWP/Collections/StackVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ public void Resize(int newCount)

private void AllocateInner(int newSize)
{
var newArray = ArrayPool<T>.Shared.Rent(newSize);
var newMemory = new Memory<T>(newArray);
_inner.CopyTo(newMemory);

if (_originalArray != null)
{
ArrayPool<T>.Shared.Return(_originalArray, clearArray: true);
}

_inner = new Memory<T>(_originalArray = ArrayPool<T>.Shared.Rent(newSize));
_originalArray = newArray;
_inner = new Memory<T>(_originalArray);
}

public void Dispose()
Expand Down

0 comments on commit 1e451ae

Please sign in to comment.