Repository navigation
Description
Current behavior 🐛
The explicit IList.Add(object) implementation on the internal ObservableVector<T> calls itself and never returns, so it kills the process with a stack overflow.
int IList.Add(object value)
{
if (value is T typedValue)
{
var ret = ((IList)this).Add(typedValue); // typedValue is boxed back to object -> resolves to IList.Add(object) again
RaiseVectorChanged(CollectionChange.ItemInserted, _list.Count - 1);
return ret;
}
...Source:
uno/src/Uno.Foundation/Collections/Internal/ObservableVector.cs
Lines 101 to 112 in e98c0dc
| int IList.Add(object value) | |
| { | |
| if (value is T typedValue) | |
| { | |
| var ret = ((IList)this).Add(typedValue); | |
| RaiseVectorChanged(CollectionChange.ItemInserted, _list.Count - 1); | |
| return ret; | |
| } | |
| else | |
| { | |
| throw new ArgumentException($"Cannot add an instance of type {value?.GetType()}"); | |
| } |
Running ((IList)new ObservableVector<int>()).Add(1); in a console app referencing Uno.Foundation gives Stack overflow. Repeated 8032 times. (Runtime-backed on an earlier master build; the code is unchanged on the sha above, confirmed by inspection.)
Expected behavior 🎯
IList.Add(object) adds the item, raises VectorChanged once, and returns the new index.
How to reproduce it (as minimally and precisely as possible) 🔬
The type is internal, but instances are handed out through public properties typed as IObservableVector<T>, so app code can reach it with a cast. For example:
- Create any Uno app (any head).
- Run:
The same applies to other public vectors backed by this type, e.g.
var brush = new Microsoft.UI.Xaml.Media.RadialGradientBrush(); var list = (System.Collections.IList)brush.GradientStops; // backed by ObservableVector<GradientStop> list.Add(new Microsoft.UI.Xaml.Media.GradientStop()); // StackOverflow, process dies
ItemsControl.GroupStyle,MediaPlaybackList.Items,CommandBarFlyout.PrimaryCommands, and to any internal code path that treats one of these vectors as a non-genericIList.
Only the plain ObservableVector<int> console repro was actually run; the GradientStops snippet is source-backed (GradientStops is new ObservableVector<GradientStop>()).
Workaround 🛠️
Use the generic Add(T) (through IList<T> / IObservableVector<T>) instead of the non-generic IList.Add.
Works on UWP/WinUI
Not applicable: this is Uno's own internal collection type.
Affected platforms 📱💻🖥️
All (shared Uno.Foundation code).
Uno.Sdk version (and other relevant versions) 📦
master @ e98c0dc
IDE version 🧑💻
N/A
Anything else we need to know? 💬
Suggested fix: call the generic overload directly. Add(T) already raises VectorChanged, so the extra RaiseVectorChanged must go too, otherwise the event fires twice:
if (value is T typedValue)
{
Add(typedValue);
return _list.Count - 1;
}A unit test in Uno.UI.UnitTests/Windows_Foundation_Collections/Given_ObservableVector.cs (it has InternalsVisibleTo) can cast to IList, call Add, and assert the item is present and VectorChanged fired once. Note that a stack overflow cannot be caught, so until fixed the test would take down the test host.
Activity