Skip to content Skip to sidebar Skip to footer

Mvvmcross Flood Of Messages: "weak Target Is Null In ... - Skipping Set"

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:

Solution 1:

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

publicoverridevoidOnDestroy()
{
    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:

publicinterfaceIActiveViewModel
{
    ///<summary>/// Used to explicitly dispose of the viewModel///</summary>voidImplode();
}

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

Tack on the new interface to class:

publicclassMyViewModel : MvxViewModel, IActiveViewModel

And add this function to the class as well:

publicvoidImplode()
{
    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; }
    protectedset
    {
        // Set data and raise property changed here
    }
}

Solution 2:

.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.

Post a Comment for "Mvvmcross Flood Of Messages: "weak Target Is Null In ... - Skipping Set""