3

I'm working with a team to create an android application using MvvmCross. Recently we've started seeing a flood of repeating warning/error messages that look something like this:

MvxBind:Warning:207.06 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
[0:] MvxBind:Warning:207.06 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
09-05 17:56:25.076 I/MvxBind (20932): 207.06 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
09-05 17:56:25.086 I/mono-stdout(20932): MvxBind:Warning:207.06 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
[0:] 
MvxBind:Warning:207.08 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
[0:] MvxBind:Warning:207.08 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
09-05 17:56:25.096 I/MvxBind (20932): 207.08 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
09-05 17:56:25.096 I/mono-stdout(20932): MvxBind:Warning:207.08 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
[0:] 
09-05 17:56:25.116 I/MvxBind (20932): 207.10 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
MvxBind:Warning:207.10 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
[0:] MvxBind:Warning:207.10 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
09-05 17:56:25.126 I/mono-stdout(20932): MvxBind:Warning:207.10 Weak Target is null in MvxTextViewTextTargetBinding - skipping set
[0:] 
09-05 17:56:25.126 I/MvxBind (20932): 207.12 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
MvxBind:Warning:207.12 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
[0:] MvxBind:Warning:207.12 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set
09-05 17:56:25.136 I/mono-stdout(20932): MvxBind:Warning:207.12 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set

These messages start about 3 minutes after switching away from a view that displays an MvxGridView. The MvxGridview is bound to a collection of view models, each with it's own bindings. The messages continue until the application is stopped. The UI gets increasingly sluggish as soon as the messages start, and eventually locks up completely. Interestingly, switching away from that screen seems to be the trigger. I can switch screens and then do nothing, for about 3 minutes, and the flood of messages will start flooding in. If I leave the app on that screen, everything runs smoothly.

Ultimately, I just want to know how to solve the issue so the app runs correctly. Some questions that might help reach that goal include:

  1. Is it possible to get a more detailed stack trace from MvvmCross? ANSWERED
  2. Do I need to manually dispose of the bindings in each cell of the MvxGridView like this: Clear bindings in MvvmCross? (The bindings bind automatically, so this might be off track.)

EDIT

This is the Environment.StackTrace variable from DebugTrace.cs. The stack trace for MvxTextViewTextTargetBinding and MvxWithEventPropertyInfoTargetBinding were exactly the same. (For readability, I removed the first few lines of the stack trace dealing with calling IMvxTrace.Trace.)

   at Cirrious.MvvmCross.Binding.Bindings.Target.MvxPropertyInfoTargetBinding.SetValue(System.Object value)
   at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.UpdateTargetFromSource(Boolean isAvailable, System.Object value)
   at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.<CreateSourceBinding>b__0(System.Object sender, Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourcePropertyBindingEventArgs args)
   at Cirrious.MvvmCross.Binding.Bindings.SourceSteps.MvxSourceStep.SendSourcePropertyChanged(Boolean isAvailable, System.Object value)
   at Cirrious.MvvmCross.Binding.Bindings.SourceSteps.MvxPathSourceStep.SourceBindingOnChanged(System.Object sender, Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourcePropertyBindingEventArgs args)
   at Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourceBinding.FireChanged(Cirrious.MvvmCross.Binding.Bindings.Source.MvxSourcePropertyBindingEventArgs args)
   at Cirrious.MvvmCross.Binding.Bindings.Source.Leaf.MvxLeafPropertyInfoSourceBinding.OnBoundPropertyChanged()
   at Cirrious.MvvmCross.Binding.Bindings.Source.MvxPropertyInfoSourceBinding.SourcePropertyChanged(System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)
   at System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod , System.Object , System.Object[] , System.Exception ByRef )
   at System.Reflection.MonoMethod.Invoke(System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(System.Object obj, System.Object[] parameters)
   at Cirrious.CrossCore.WeakSubscription.MvxWeakEventSubscription`2[[System.ComponentModel.INotifyPropertyChanged, System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.ComponentModel.PropertyChangedEventArgs, System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnSourceEvent(System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)
   at Cirrious.MvvmCross.ViewModels.MvxNotifyPropertyChanged+<>c__DisplayClass1.<RaisePropertyChanged>b__0()
   at Cirrious.CrossCore.Core.MvxMainThreadDispatcher.ExceptionMaskedAction(System.Action action)
   at Cirrious.MvvmCross.Droid.Views.MvxAndroidMainThreadDispatcher+<>c__DisplayClass1.<RequestMainThreadAction>b__0(System.Object ignored)
   at Android.App.SyncContext+<Post>c__AnonStorey2E.<>m__1F() in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:line 23
   at Java.Lang.Thread+RunnableImplementor.Run() in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:line 32
   at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this) in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Java.Lang.IRunnable.cs:line 71
   at System.Object.ea853478-e8d0-4bbe-8306-98c58e64097e(IntPtr , IntPtr )

2 Answers 2

2

I had to override the OnDestory() method in the view that displays the MvxGridView.

public override void OnDestroy()
{
    MyProject.Core.ViewModel.MyViewModel vm = this.DataContext as MyProject.Core.ViewModel.MyViewModel;            
    base.OnDestroy();
    vm.Implode();
}

Next I created an interface that looks like this:

public interface IActiveViewModel
{
    /// <summary>
    /// Used to explicitly dispose of the viewModel
    /// </summary>
    void Implode();
}

The rest of the code goes in the associated View Model MyViewModel.

Tack on the new interface to class:

public class MyViewModel : MvxViewModel, IActiveViewModel

And add this function to the class as well:

public void Implode()
{
    if (MyGridTileViewModels != null)
    {
        foreach (GridTileViewModel vm in MyGridTileViewModels)
        {
            vm.Dispose();
        }
    }
    this.Dispose();
}

MyGridTileViewModels is the property the MvxGridView is bound to, and it is defined like this:

private ObservableCollection<GridTileViewModel> _myGridTileViewModels = null;

public ObservableCollection<GridTileViewModel> MyGridTileViewModels 
{
    get { return _myGridTileViewModels; }
    protected set
    {
        // Set data and raise property changed here
    }
}
1

.1. Is it possible to get a more detailed stack trace from MvvmCross?

Your app is probably already providing it's own DebugTrace implementation - so you can provide any additional trace information you want to there within your implementation. In C# Stack trace is available using Environment.StackTrace (but I've never used this in MonoDroid - but I assume it works!)

.2. Do I need to manually dispose of the bindings in each cell of the MvxGridView ... ?

The MvxGridView bindings will generally be stored in the owning Activity (or Fragment). These should be cleared up when that Activity is OnDestroyed - see https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid/Views/MvxBindingActivityAdapter.cs#L49

The bindings in each cell will be stored in the cell. These should be cleared up when that cell is Disposed - https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBaseListItemView.cs#L42 - the timing of this is down to Xamarin.Android, but since v4 MonoDroid has guaranteed that it implements IDisposable correctly on all Java.Lang.Object derived objects (see 4.0 release notes - http://docs.xamarin.com/releases/android/mono_for_android_4/mono_for_android_4.0.0)

My guess (from "is bound to a collection of view models, each with it's own bindings") is that maybe your grid cells are binding to objects which have lives longer than the View/ViewModel somehow - that somehow the ViewModels and their bindings are living on longer than the lifetime of their containing View/Activity.

You can, of course, clear any bindings early if you want to - although I don't have any advice in your particular case - too little code to currently go on. As a first step I would suggest working out what is keeping your ViewModels in memory while your Views seem to be successfully disappearing. Your detailed stack trace may help with that.

1
  • I added a stack trace to my answer. Does that reveal anything to you? Changing the ViewModel only changes the View without destroying the activity. The project is modeled after this example that demonstrates dynamically swapping the views with help from a custom presenter: MultiRegionPresenter. The key elements are the tags on each view, MultiRegionPresenter.Show(...), and MainActivity.Show(...). Regions are defined in MainActivity.xaml. Could this method cause MvvmCross to get confused because the view changes and the activity stays?
    – Josh
    Sep 6, 2013 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.