MvvmCross: NullReferenceException in MvxPathSourceStep.ClearPathSourceBinding

I’ve stumbled upon an interesting crash today. The crash occured in ClearPathSourceBinding. This is the first time it crashes, in 3 months usage of the app.

    private void ClearPathSourceBinding()
    {
        if (this._sourceBinding != null)
        {
            this._sourceBinding.Changed -= this.SourceBindingOnChanged;
            this._sourceBinding.Dispose();
            this._sourceBinding = null;
        }
    }

So the only way this can crash, is because it is executed twice in 2 concurrent tasks. The stacktrace was:

MvxPathSourceStep.OnDataContextChanged
MvxSourceStep.DataContext (setter)
MvxFullBinding.DataContext (setter)
MvxTaskBasedBindingContext.OnDataContextChange
Task.Execute (no information from where it has been created)

This information is not enough to find the root cause of this problem: i don’t know which binding is concerned.

In OnDataContextChange, the DataContext setter is not called from a Task. It is called directly from the DataContext setter. But inside OnDataContextChange are called DataContext setters from bindings. This should mean it is called from inside the Task of OnDataContextChange.

This means a lock should be added around the code inside MvxPathSourceStep.OnDataContextChanged

   private object syncLock = new object();

    protected override void OnDataContextChanged()
    {
        lock(syncLock) {
            this.ClearPathSourceBinding();
            this._sourceBinding = this.SourceBindingFactory.CreateBinding(this.DataContext, this.Description.SourcePropertyPath);
            if (this._sourceBinding != null)
            {
                this._sourceBinding.Changed += this.SourceBindingOnChanged;
            }
        }
        base.OnDataContextChanged();
    }

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (12 by maintainers)

Most upvoted comments