[Gtk-sharp-list] Crash when working with multiple TreeViews alongside WebView

Chris Howie cdhowie at gmail.com
Sun Jul 12 14:34:02 EDT 2009


On Sun, Jul 12, 2009 at 12:56 PM, Andorien<andorien at gmail.com> wrote:
> So I'm trying to put together a simple RSS feed reader as sort of a personal
> project. It uses the standard design of a feed list to the left, a list of
> articles on top, and the actual article being displayed on bottom. The
> desired behavior is that selecting a feed will clear the article list of any
> previous entries and populate it with the feed's articles. Selecting an
> article then loads the Webkit.WebView widget with the html string from the
> article for display. The crash occurs if you have a feed as well as an
> article already selected, and you click another feed to select. This causes
> an exception to be thrown with the following text:
>
> System.NullReferenceException: Object reference not set to an instance of an
> object
>
> It should be noted that this crash doesn't occur when selecting different
> feeds as long as an article hasn't been selected yet. Here are the relevant
> functions:
>
> void OnFeedSelectionChanged(object o, EventArgs args)
> {
> 	TreeSelection selection = (TreeSelection) o;
> 	TreeIter iter;
> 	selection.GetSelected(out iter);
> 			
> 	Feed feed = (Feed) FeedList.Model.GetValue(iter, 0);
> 			
> 	ItemList.View.Selection.UnselectAll(); // <-- Crash occurs at both this
> function call and the next
> 	ItemList.Model.Clear();
> 			
> 	for(byte n = 0; n < feed.Items.Count; n++)
> 		ItemList.Model.AppendValues(feed.Items[n]);
> }
>
> void OnItemSelectionChanged(object o, EventArgs args)
> {
> 	TreeSelection selection = (TreeSelection) o;
> 	TreeIter iter;
> 	selection.GetSelected(out iter);
> 			
> 	Feed.FeedItem item = (Feed.FeedItem) ItemList.Model.GetValue(iter, 0);
> 			
> 	View.LoadHtmlString(item.Contents, "");
> }
>
> Interestingly enough, if I remove the code loading the WebView widget with
> HTML, the crash disappears. This initially led me to believe that there was
> a problem with WebView, but replacing it with gtk.HTML resulted in the same
> problem. I figure there must be some issue with the why I'm manipulating the
> two TreeViews, but I have no idea. Any help would be greatly appreciated.

The problem here is rather simple.  When you call UnselectAll, you are
causing a selection change to occur.  OnItemSelectionChanged is being
called to handle this, but nowhere do you handle the case where there
is no item selected.  Here is what's happening:

/* This method is returning false, because there is no selection.  You
should alter your code path to handle this.  Because you do not, iter
is set to an invalid/zero TreeIter. */
selection.GetSelected(out iter);

/* This line works fine, because GetValue is returning null, since the
iter is invalid.  You can cast null to any reference type
successfully. */
Feed.FeedItem item = (Feed.FeedItem) ItemList.Model.GetValue(iter, 0);

/* This line crashes because item is null. */
View.LoadHtmlString(item.Contents, "");

Simply handle the case where selection.GetSelected is returning false
and you should have no issues.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers


More information about the Gtk-sharp-list mailing list