Skip to content

NullReferenceException at BindingExpression #32602

Closed

Description

@Toomas75

Description

In app log:

System.NullReferenceException:Object reference not set to an instance of an object at Microsoft.Maui.Controls.WeakEventProxy2[[System.ComponentModel.INotifyPropertyChanged, System.ObjectModel, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[System.ComponentModel.PropertyChangedEventHandler, System.ObjectModel, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].TryGetHandler(PropertyChangedEventHandler& )
at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
at SomeViewModel.SomeProperty.OnPropertyChanged(String propertyName)`

could be race condition in _expression.Apply() (Dispatcher.Dispatch)
https://github.com/dotnet/maui/blob/bf96ba698d8abb3b5b69396c905abc0078280b20/src/Controls/src/Core/BindingExpression.cs#L713

possible fix=> obj.Dispatcher.DispatchIfRequired(() => _expression?.Apply());

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

9.0.110 SR12

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

trying to update bound properties on the UI thread.

Relevant log output

Activity

  1. added
    t/bug
    on Nov 13, 2025
  2. added
    area-xaml
    on Nov 14, 2025
  3. added this to the .NET 10 Servicing milestone on Nov 14, 2025
  4. StephaneDelcroix commented on Nov 14, 2025

    @StephaneDelcroix
    Contributor

    do you have a reproduction project ?

  5. added
    s/needs-repro
    on Nov 14, 2025
  6. Toomas75 commented on Nov 18, 2025

    @Toomas75
    Author

    do you have a reproduction project ?

    No. I've only seen this error a couple of times in the production app log(rare). Last time the property was bound to CollectionView.SelectedItem. Property updated in non UI thread.

    Can't reproduce myself. But it happens.

  7. added
    s/needs-attention
    and removed
    s/needs-repro
    on Nov 18, 2025
  8. MichaelLHerman commented on Jun 2, 2026

    @MichaelLHerman

    Seeing this in 9.0.120

    Don't have a minimally reproducible sample

    Exception from Sentry:
    System.NullReferenceException: Object reference not set to an instance of an object
    at bool WeakEventProxy<INotifyPropertyChanged, PropertyChangedEventHandler>.TryGetSource(out INotifyPropertyChanged source)() in WeakEventProxy.cs:line 27
    at void WeakPropertyChangedProxy.Unsubscribe()() in BindingExpression.cs:line 602
    at void WeakPropertyChangedProxy.OnPropertyChanged(object sender, PropertyChangedEventArgs e)() in BindingExpression.cs:line 615
    at void BindableObject.OnPropertyChanged(string propertyName)() in BindableObject.cs:line 408
    at void Element.OnPropertyChanged(string propertyName)() in Element.cs:line 691
    at void BindableObject.OnBindablePropertySet(BindableProperty property, object original, object value, bool didChange, bool willFirePropertyChanged)() in BindableObject.cs:line 675
    at void Element.OnBindablePropertySet(BindableProperty property, object original, object value, bool changed, bool willFirePropertyChanged)() in Element.cs:line 668
    at void BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, bool silent)() in BindableObject.cs:line 663
    at void BindableObject.SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)() in BindableObject.cs:line 591
    at void TypedBinding<IChargeDetailsViewModel, Patient>.ApplyCore(object sourceObject, BindableObject target, BindableProperty property, bool fromTarget, SetterSpecificity specificity)() in TypedBinding.cs:line 325
    at void TypedBinding<IChargeDetailsViewModel, Patient>.ApplyToResolvedSource(object source, BindableObject target, BindableProperty targetProperty, bool fromBindingContextChanged, SetterSpecificity specificity)() in TypedBinding.cs:line 248
    at void TypedBinding<IChargeDetailsViewModel, Patient>.Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged, SetterSpecificity specificity)() in TypedBinding.cs:line 199
    at void BindableObject.ApplyBindings(bool fromBindingContextChanged)() in BindableObject.cs:line 693
    at void BindableObject.SetInheritedBindingContext(BindableObject bindable, object value)() in BindableObject.cs:line 367
    at void Element.SetChildInheritedBindingContext(Element child, object context)() in Element.cs:line 827
    at void Element.OnBindingContextChanged()+(BindableObject child, object bc) => { }() in Element.cs:line 603
    at void BindableObjectExtensions.PropagateBindingContext(BindableObject self, IEnumerable children, Action<BindableObject, object> setChildBindingContext)() in BindableObjectExtensions.cs:line 48
    at void Element.OnBindingContextChanged()() in Element.cs:line 601
    at void VisualElement.OnBindingContextChanged()() in VisualElement.cs:line 1366
    at void View.OnBindingContextChanged()() in View.cs:line 263
    at void BindableObject.SetInheritedBindingContext(BindableObject bindable, object value)() in BindableObject.cs:line 368
    at void Element.SetChildInheritedBindingContext(Element child, object context)() in Element.cs:line 827
    at void Element.SetParent(Element value)() in Element.cs:line 432
    at void Element.OnChildAdded(Element child)() in Element.cs:line 620
    at void VisualElement.OnChildAdded(Element child)() in VisualElement.cs:line 1375
    at void Element.AddLogicalChild(Element element)() in Element.cs:line 225
    at void Layout.Add(IView child)() in Layout.cs:line 163
    at void ChargeDetailsPage.InitializeComponent()() in Views_Hospital_ChargeDetailsPage.xaml.sg.cs:line 35
    at new ChargeDetailsPage(ChargeDetailsParameter parameter, IChargeDetailsViewModel chargeDetailsViewModel, IMessenger messenger, ISecurityService securityService, IXamarinForms xamarinForms)() in ChargeDetailsPage.xaml.cs:line 33
    at object ConstructorInvoker.InterpretedInvoke(object obj, IntPtr* args)()
    at object ConstructorInvoker.InvokeWithManyArgs(Span)()
    at object ConstructorInvoker.Invoke(Span)()
    at object ConstructorMatcher.CreateInstance(IServiceProvider)()
    at object ActivatorUtilities.CreateInstance(IServiceProvider, Type, params object[])()
    at ChargeDetailsPage ActivatorUtilities.CreateInstance(IServiceProvider, params object[])()
    at async Task NavigationService.ToChargeDetails(string hospitalStayId, string chargeId, bool makeCopy, bool animate, ENavigationType navigationType)() in NavigationService.cs:line 217
    at async void AsyncRelayCommand.AwaitAndThrowIfFailed(Task)() in AsyncRelayCommand.cs:line 351
    at void Task.ThrowAsync(Exception, SynchronizationContext)+(object state) => { }()
    at void NSAsyncSynchronizationContextDispatcher.Apply()() in NSAction.cs:line 167
    at void Registrar_Callbacks.callback_4248_Foundation_NSAsyncSynchronizationContextDispatcher_Apply(IntPtr pobj, IntPtr sel, IntPtr* exception_gchandle)()

    Suspect this is due to having Fody.PropertyChanged in my project. Seeing if excluding a databound ContentView from PropertyChanged-weaving fixes the issue

  9. baaaaif commented on Aug 18, 2026

    @baaaaif
    Contributor

    We are still seeing the same underlying WeakEventProxy race in production on .NET MAUI 10.

    Our Android application uses Microsoft.Maui.Controls 10.0.90 and .NET runtime 10.0.11. This Sentry issue has 167 occurrences affecting 30 users; the latest occurrence was on Android 16 (API 36). There are no application frames in the stack trace:

    System.NullReferenceException
      at WeakEventProxy<INotifyPropertyChanged, PropertyChangedEventHandler>.TryGetSource(...)
      at WeakPropertyChangedProxy.Unsubscribe()
      at BindingExpressionPart.Finalize()
    

    This strongly suggests the same field-read race described here, but on _source during finalization/unsubscription rather than on _handler while handling PropertyChanged. Unsubscribe() sets _source and _handler to null; another concurrent path can therefore observe a non-null field in the condition and then dereference it after it has been cleared.

    The current main branch still has this pattern in both methods:

    if (_source is not null && _source.TryGetTarget(out source))

    A minimal defensive fix is to capture the weak-reference field once before testing it (and apply the same change to TryGetHandler):

    public bool TryGetSource([MaybeNullWhen(false)] out TSource source)
    {
        var sourceReference = _source;
        if (sourceReference is not null && sourceReference.TryGetTarget(out source))
        {
            return source is not null;
        }
    
        source = default;
        return false;
    }
    
    public bool TryGetHandler([MaybeNullWhen(false)] out TEventHandler handler)
    {
        var handlerReference = _handler;
        if (handlerReference is not null && handlerReference.TryGetTarget(out handler))
        {
            return handler is not null;
        }
    
        handler = default;
        return false;
    }

    This removes the time-of-check/time-of-use window without changing the existing weak-event semantics. Since this can occur from a finalizer, it is an unhandled process-terminating crash on Android rather than a recoverable binding error.

  10. added a commit that references this issue on Aug 18, 2026
    b60e6ee
  11. 13 remaining items

  12. added 6 commits that reference this issue on Sep 27, 2026
    be329ad
    209f83d
    e2b234a
    504cc63
    427b39d
    d4eb2fd
  13. kubaflo commented on Oct 10, 2026

    @kubaflo
    Contributor

    Thank you for the report and the investigation details. We are closing this as completed: WeakEventProxy captures its source and handler before checking and dereferencing them, removing the exact BindingExpression null-reference race. This records the inflight/current repair, not a claim that every package contains it.

    Supporting evidence: #37568 (merged into inflight/current).

    This records the merged source change; it does not imply that the fix is present in every released .NET MAUI package.

    If you can still reproduce the reported behavior in a build that includes the linked change, please comment here with the exact version and reproduction details so we can reopen and investigate. The existing reproduction and discussion remain available; a new issue is not required.

  14. added 6 commits that reference this issue on Oct 13, 2026
    21451e8
    82a14db
    fac6e78
    8e39ad3
    b0f0426
    0135dda
  15. locked and limited conversation to collaborators on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamls/needs-attentiont/bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions