Search code examples
c#optimizationwin-universal-apprelease-mode.net-native

UWP unhandled exception only in Release mode


I've been struggling for a week with a bug in my published UWP app that only seems to manifest itself in release mode when deployed on phones (ARM target). It randombly crashes the app when navigating from one specific page to another. It only affects those two specific pages, even if their structure is identical to many others in the project that never show this behavior (usage of the OnNavigatedTo and OnLoaded to present the content and fill the ViewModel, etc)

The only clue I have (since I'm unable to get a full stacktrace) is this error message, which I am able to get with great difficulty by trial and error:

[Exception](http://i.imgur.com/9Mpejje.jpg)

It seems to be pretty generic and I've tried several things I found when looking for it:

  • Ensuring my IValueConverters never return null or unexpected values.
  • Ensuring no variables are left uninitialized.
  • Ensuring the UI is completely loaded before accessing any of its components.
  • Ensuring there are no UI manipulations outside of the Dispatcher thread.
  • Updating my project libraries (Newtonsoft JSON and winfbsdk, while the latter is not used in neither of the views).

After a lot of reading I tried to fiddle with the compilation options in the project settings, and found out that the offending configuration is having activated both the .NET native toolchain compilation AND the code optimization checkboxes. I managed to reproduce the crash in Debug mode with those options activated (basically playing with the app until randomly the bug would appear) but due to precisely those settings being activated, I got no stacktrace. The debugger simply breaks on the Unhandled XAML exception line and even the sender and e arguments of the method that launches it are "not accesible at the moment".

On the other hand, I got a few minidumps on my phone that I am unable to debug in Visual Studio 2015. It can't find the kenelbase.pdb symbols, even if I have the Microsoft Symbol Servers checked on my configuration.

I can't to upload the app to the store even if I left the .NET native compilation option checked (it seems to need both, the website throws an error during validation otherwise), so my users will eventually run into the bug. If anyone has any pointers or ideas, I would be really grateful.

Edit 0:

VS Enterprise 2015, W10M compilation 10586.107 running on a Lumia 925. I wish I'd knew why it doesn't update to .164, though.

Edit 1:

I have noticed that when the crash happens, none of several images I have on my view get loaded. There are some BitmapIcons loaded from bundled content (png) and two images loaded from network using an IValueConverter to turn them into BitmapImage and therefore, ImageSources. I have checked and rechecked the converter for nulls and exceptions, should be clear.

Edit 2:

Parent view, event that triggers navigation:

    private void SessionList_ItemClick(object sender, ItemClickEventArgs e)
    {
        Frame.Navigate(typeof(ClubPage), e.ClickedItem); // The collection is binded, so the clicked item is a model
    }

Code executed on the crashing view:

    private void ClubPage_Loaded(object sender, RoutedEventArgs e)
    {
        isInfoShowing = false;
        isLogoZoomed = false;
        compositor = ElementCompositionPreview.GetElementVisual(sender as UIElement).Compositor;
        Visual status = ElementCompositionPreview.GetElementVisual(StatusBlock);
        status.Opacity = 0.0f;
        ClubViewModel.Current.ShowIn = false;
        ClubViewModel.Current.ShowComing = false;
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter != null)
        {
            ClubViewModel.Current.Club = e.Parameter as Club;
            SplitViewShellViewModel.Current.Title = ClubViewModel.Current.Club.Name.Replace("_", " ");
            SplitViewShellViewModel.Current.TitleBarOpacity = 1.0f;
            ClubViewModel.Current.InLimit = App.RefreshLimit;
            ClubViewModel.Current.ComingLimit = App.RefreshLimit;
            await ClubViewModel.Current.GetClub(); //I'm positive that up until this point the code is executed
            await ClubViewModel.Current.GetPeopleIn();
            await ClubViewModel.Current.GetClubNotifications();
        }
    }

Edit 3:

Activating Native debugging gives me the following stacktrace:

enter image description here

The Exception is raised when a DispatcherTimer ticks (I have none in my view, everything is internal). Besides that, I couldn't make any sense of it.

I have tried everything I could think of and I also refactored my whole view. The only "strange" thing I'm doing is a ListView inside a container that breaks its virtualization, but I'm not concerned about performance as the list is really small.


Solution

  • This is incredibly odd (for me at least), but apparently the navigation code shown above wasn't executing on the Dispatcher thread...even if it was triggered by an UI event. Sending the call to the dispatcher as follows did the trick:

    private async void SessionList_ItemClick(object sender, ItemClickEventArgs e)
    {
       await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(ClubPage), e.ClickedItem)); 
    }
    

    Identical code (without the explicit Dispatcher.RunAsync wrapper) spread across the whole app works as expected. Every time I've run into UI thread issues the exceptions thrown were managed and easy to reproduce...what could possibly be happening during the compiler optimization process that could trigger this?