diff --git a/src/Controls/src/Core/BindableObject.cs b/src/Controls/src/Core/BindableObject.cs index e1b2d409257c..ebae38c2eb15 100644 --- a/src/Controls/src/Core/BindableObject.cs +++ b/src/Controls/src/Core/BindableObject.cs @@ -6,7 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using Microsoft.Extensions.Logging; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Dispatching; @@ -721,20 +720,19 @@ BindablePropertyContext CreateContext(BindableProperty property) [MethodImpl(MethodImplOptions.AggressiveInlining)] BindablePropertyContext GetOrCreateContext(BindableProperty property) { -#if NETSTANDARD var context = GetContext(property); if (context is null) { + // Create the context before adding it to _properties. CreateContext can invoke a + // DefaultValueCreator that re-entrantly sets other properties, which may resize + // _properties and reallocate its backing storage. Holding a ref into the dictionary + // across that call (e.g. via CollectionsMarshal.GetValueRefOrAddDefault) would leave + // the ref pointing at the discarded array, so the created context would never be + // stored. See https://github.com/dotnet/maui/pull/35970. context = CreateContext(property); _properties.Add(property.InternalId, context); } -#else - ref var context = ref CollectionsMarshal.GetValueRefOrAddDefault(_properties, property.InternalId, out var exists); - if (!exists) - { - context = CreateContext(property); - } -#endif + return context; } diff --git a/src/Controls/tests/Core.UnitTests/BindableObjectUnitTests.cs b/src/Controls/tests/Core.UnitTests/BindableObjectUnitTests.cs index f1f9316cf5e7..cebabb54bb21 100644 --- a/src/Controls/tests/Core.UnitTests/BindableObjectUnitTests.cs +++ b/src/Controls/tests/Core.UnitTests/BindableObjectUnitTests.cs @@ -1564,6 +1564,65 @@ public void GetValues() Assert.Equal(5, values[2]); } + [Fact] + public void GetValuesReturnsSetStateAndValue() + { + var prop = BindableProperty.Create("Foo", typeof(int), typeof(MockBindable), 0); + var prop1 = BindableProperty.Create("Foo1", typeof(int), typeof(MockBindable), 1); + var prop2 = BindableProperty.Create("Foo2", typeof(int), typeof(MockBindable), 2); + var bindable = new MockBindable(); + + bindable.SetValue(prop, 3); + bindable.SetValue(prop2, 5); + + var values = bindable.GetValues(new[] { prop, prop1, prop2 }); + + Assert.Equal(3, values.Length); + Assert.True(values[0].IsSet); + Assert.Equal(3, values[0].Value); + Assert.False(values[1].IsSet); + Assert.Equal(0, values[1].Value); + Assert.True(values[2].IsSet); + Assert.Equal(5, values[2].Value); + } + + [Fact] + public void DefaultValueCreatorCachesValueWhenReentrantPropertyAddsResizeStore() + { + var reentrantProperties = new BindableProperty[8]; + for (var i = 0; i < reentrantProperties.Length; i++) + { + reentrantProperties[i] = BindableProperty.Create($"Reentrant{i}", typeof(int), typeof(MockBindable), 0); + } + + var defaultValueCreatorInvocations = 0; + var propertyWithCreator = BindableProperty.Create( + "ReentrantDefault", + typeof(int), + typeof(MockBindable), + 0, + defaultValueCreator: b => + { + defaultValueCreatorInvocations++; + for (var i = 0; i < reentrantProperties.Length; i++) + { + b.SetValue(reentrantProperties[i], i + 1); + } + + return 42; + }); + + var bindable = new MockBindable(); + + var first = (int)bindable.GetValue(propertyWithCreator); + var second = (int)bindable.GetValue(propertyWithCreator); + + Assert.Equal(42, first); + Assert.Equal(42, second); + Assert.Equal(1, defaultValueCreatorInvocations); + Assert.True(bindable.IsSet(propertyWithCreator)); + } + class BindingContextConverter : IValueConverter {