Object reference not set to an instance of an object. -> ...Dispose();

Mar 31, 2011 at 9:50 AM

Hello,
I am currently working as a new media designer on a project which uses Cinch as a core pattern. As the project is getting more complex and complex (also on the UI side) I starting getting strange errors in Blend, which do not occur in Visual Studio 2010. The project itself compiles fine under VS2010 and also under Expression Blend 4, but the Designer stopped working giving me a "NullReferenceException" - Object reference not set to an instance of an object.

I used VS2010 to debug Blend itself (Attach process to...) and found following line to throw this strange error (one of three, all the same - occuring only in UserControls):

this.Dispatcher.InvokeIfRequired(() => ((ICinchDisposable)this.DataContext).Dispose());

which is in the following codebehind file:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Cinch;

namespace ACME.Library.Views.Views.SelectViews
{
    /// <summary>
    /// Interaction logic for SearchView.xaml
    /// </summary>
       [ViewnameToViewLookupKeyMetadata("SearchView",typeof(SearchView ))]
    public partial class SearchView : UserControl,IWorkSpaceAware
    {
        public SearchView()
        {
            InitializeComponent();
        }

        ~SearchView()
        {
            this.Dispatcher.InvokeIfRequired(() => ((ICinchDisposable)this.DataContext).Dispose());
        }

        #region IWorkSpaceAware Members

        public static readonly DependencyProperty WorkSpaceContextualDataProperty =
            DependencyProperty.Register("WorkspaceContextualData", typeof(object), typeof(SearchView));

        public WorkspaceData WorkSpaceContextualData
        {
            get
            {
                return (WorkspaceData)GetValue(WorkSpaceContextualDataProperty);
            }
            set
            {
                SetValue(WorkSpaceContextualDataProperty, value);
            }
        }

        #endregion
    }
}

And this is the XAML:
<UserControl x:Class="ACME.Library.Views.Views.SelectViews.SearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			 xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
             meffed:ViewModelLocator.ViewModel="SearchVM"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="300">
    <Grid Margin="0,0,0,0">
            	<Label Content="Enter Search:" HorizontalAlignment="Left" Margin="29,21,0,0" VerticalAlignment="Top"/>
    	<TextBox Margin="130,25,12,0" TextWrapping="Wrap" Text="{Binding SearchText}" VerticalAlignment="Top" />
    	<Button Content="Search" Command="{Binding SearchBtn}" HorizontalAlignment="Left" Margin="29,67,0,0" VerticalAlignment="Top" Width="75"/>
    	<ListBox ItemsSource="{Binding Path=SearchHistory}" SelectedItem="{Binding SelectSearchText}" Margin="130,96,12,25" />
    </Grid>
</UserControl>

Any idea how to solve this?

 

Mar 31, 2011 at 3:18 PM

I honestly can't see anything wrong with that, what happens if you put break point in destructor, what object is NULL?

 

Like I say can't see anything up with that at all, and it is similar to what I do in the CinchV2 WPF demo, except I do not use the Dispatcher.

 

What happens if you do not use Dispatcher, what might happen is that the Dispatcher schedules it later (as you are not specifying a DispatcherPriority to Invoke on, it may be scheduled for later), but the View may get finialized so MEF releases Part (ViewModel) so it may also be GC'd so when the Dispatcher.Invoke happens ViewModel is NULL.

 

I am not really sure about that, but I would try it without the use of the Dispatcher and see what happens then.