ButchersBoy / Dragablz

Dragable and tearable tab control for WPF
http://dragablz.net
MIT License
2.2k stars 323 forks source link

GetOrderedTabHeaders() - Object reference not set to an instance of an object #211

Open RickStrahl opened 6 years ago

RickStrahl commented 6 years ago

In Markdown Monster I'm running into a lot of user errors with Object reference not set to an instance of an object. in my logs related to a call to GetOrderedTabHeaders().

I've not been able to replicate this myself, but I get about 50 errors or so a day from this method from users in my analytics logs. I fenced off that code now to capture the error and ignore the ordered headers.

More info:

I call GetOrderedHeaders() as part of my application shut down sequence which is fired off the main form's OnClosing() handler. It is called as part of code that's responsible for saving the open tab file names and related files that are currently open. I recently switched from just iterating the tabs which hold a Tag and the document, and then matching them to my recent files, but in that process the tab order was getting lost. So I switched to GetOrderedTabHeaders() and now these errors are popping up quite frequently.

I added the PDB to the distribution and it looks like the failure is happening in that one line in here:

        /// <summary>
        /// Gets the header items, ordered according to their current visual position in the tab header.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DragablzItem> GetOrderedHeaders()
        {
            return _dragablzItemsControl.ItemsOrganiser.Sort(_dragablzItemsControl.DragablzItems());
        }

I realize this is very vague since I haven't been able to reproduce it myself, but I thought I'd report it here in case you can perhaps see an issue. My guess would be that ItemsOrganizer is the null reference here and at the very least maybe there should be some null checks in here most likely for the ItemsOrganizer and if that's not defined create it or return unordered headers instead?

RickStrahl commented 6 years ago

So after a bit of further debugging, using private Reflection and logging in production with Analytics it turns out its the _DragabalzItemsControl that is null.

I still can't duplicate the scenario locally but I see tons of these error in my logs. Unfortunately the call stack is not helping here because the form is shutting down (onClosing()), but I've tried all sorts of scenarios to make it fail for me and can't.

I have a workaround by just grbbing the unordered headers and that works, but it'd be nice to get GetOrderHeaders() to work reliably.