Cysharp / ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
MIT License
602 stars 46 forks source link

ObservableList.AddRange(IEnumerable) adds nulls due to a bug in CloneCollection #38

Closed jphorv-bdo closed 5 months ago

jphorv-bdo commented 7 months ago

I discovered this while using ObvervableList.AddRange(IEnumerable<T> items), which uses CloneCollection to turn the IEnumerable into a known-length collection as efficiently as possible.

In CloneCollection there is a bug inside the filling loop when it calls TryEnsureCapacity(ref array, i) on the array being filled.

Here TryEnsureCapacity() allocates a larger array without copying the old array items into the new one. As a result, the previous array contents are lost and are null in the final result.

Workaround: it works if I call ToArray() on my IEnumerable so that it will call ObservableList.AddRange(T[] items) instead.

jphorv-bdo commented 5 months ago

Confirmed fixed by PR #43 , thank you!