1

I am developing a Windows Phone 8.1 app. It works fine on WP 8 and WP 8.1 devices, but on the device with Windows 10 it throws

ExecutionEngineException was unhandled. An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.

in various sections in both "Debug" and "Release" without any data about what went wrong. There are some places, where the exception is always thrown and some, where it's thrown from time to time. Example code below throws the exception - it is basically a method to switch between tabs, which are StackPanels when the button (Grid with Image) is tapped:

private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
    if(!isMapVisible)
    {
        hideSection();
        map_wrapper.Visibility = Windows.UI.Xaml.Visibility.Visible;
        map_button.Background = new SolidColorBrush(ColorHelper.FromArgb(0xFF, 40, 110, 73));
        map_icon.Source = new BitmapImage(new Uri(FileHelper.getIconPath("tu_2.png")));
        isMapVisible = true;
    }
}

private void hideSection()
{
    if(isMapVisible)
    {
        map_button.Background = new SolidColorBrush(ColorHelper.FromArgb(0xFF, 238, 238, 238));
        map_icon.Source = new BitmapImage(new Uri(FileHelper.getIconPath("tu.png")));
        isMapVisible = false;
        map_wrapper.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }
    else if(isPhotoVisible)
    {
        photo_button.Background = new SolidColorBrush(ColorHelper.FromArgb(0xFF, 238, 238, 238));
        photo_icon.Source = new BitmapImage(new Uri(FileHelper.getIconPath("photo_green.png")));
        isPhotoVisible = false;
        image_wrapper.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }
    else if(isListVisible)
    {
        list_button.Background = new SolidColorBrush(ColorHelper.FromArgb(0xFF, 238, 238, 238));
        list_icon.Source = new BitmapImage(new Uri(FileHelper.getIconPath("!2.png")));
        isListVisible = false;
        news_wrapper.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }
}
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
Piotr Mitkowski
  • 282
  • 3
  • 13
  • Can you mark the lines where the exception is thrown? – Stefan Jul 06 '16 at 08:34
  • I can't because I don't know that. I only know that I get the exception after the grid is tapped so when the method above is triggered. The exception doesn't have any additonal info and also the Call Stack window is empty. – Piotr Mitkowski Jul 06 '16 at 08:37
  • What's the size of your images? – Stefan Jul 06 '16 at 08:39
  • And, what is this `!` in `getIconPath("!2.png")`? – Stefan Jul 06 '16 at 08:40
  • And why are you reloading the images all the time? It's better to cache them if possible. – Stefan Jul 06 '16 at 08:41
  • It's a part of filename. The images in that section are 600 x 306 – Piotr Mitkowski Jul 06 '16 at 08:42
  • Ah ok, sorry about the `!`, somehow I thought this was an invalid character :| – Stefan Jul 06 '16 at 08:43
  • I will implement the advice about caching the images, however it probably won't fix the error – Piotr Mitkowski Jul 06 '16 at 08:44
  • 1
    The error is probably thrown from some unmanaged part of your program. Bitmap helpers and image de-compressors are a typical suspect. It might also be possible that one of your images is corrupted. See http://stackoverflow.com/questions/2013310/an-unhandled-exception-of-type-system-executionengineexception-occurring-when and http://stackoverflow.com/questions/967044/system-executionengineexception-failure for simular issueses – Stefan Jul 06 '16 at 08:46
  • While your at it: cache the Brushes as well. Although they are relatively small they are likely to leak memory if not disposed properly. – Stefan Jul 06 '16 at 08:47
  • But is it possible, that the unmanaged part doesn't cause any problems on Windows Phone 8/8.1 and does on Windows 10? – Piotr Mitkowski Jul 06 '16 at 08:54
  • Good question. It can be caused by device and os dependent variables. Like amount of memory or .net framework versions (implementation or beta release). So, although it shouldn't make any difference, in reality it does. And usually they are the most annoying bugs you'll encounter XD – Stefan Jul 06 '16 at 08:59

4 Answers4

3

Finally I managed to fix the code. However the error wasn't in the code above. I used something called "Safe Navigation". The example is shown in the code below:

Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
    rootFrame.Navigate(typeof(MainPage));
});

I also handled all asynchronous methods with await operator (I had left some of them before to run asynchronously). One of those improvements fixed the error.

Piotr Mitkowski
  • 282
  • 3
  • 13
0

I know this comes over two years beyond the original post, but maybe this will help someone looking for an answer to this question. I kept getting apparently random System.ExecutionEngineExceptions from a Window 10 UWP desktop app. After several days, I found the answer for my particular problem. I had used a MVVM platform, and the x:UID in one of my views had become corrupted.

IT SHOULD HAVE BEEN: <TextBlock x:Uid="Battery ...

IT WAS: <TextBlock x:Uid="=Battery"

The error was not flagged as a XAML problem, as many syntax errors similar to this are but once I corrected that error, by removing the unneeded equal sign, the exception went away.

Hope this helps someone else.

Clyde

0

Also want to add to the above. The XAML engine does not check for duplicate x:Uid and I also got this error when I had two x:Uid's with the same name. Made all x:Uid's throughout my project unique (unfortunately expands the resource file) but that resolved any further issues. Wished that the XAML designer checked for duplicate x:Uid's the was it does for x:Name's.

Again, hope it helps someone in the future.

Cheers,

Clyde

0

For those encountering ExecutionEngineException while using Xamarin, make sure you have the latest version of Microsoft.NETCore.UniversalWindowsPlatform.

The issue was fixed in UWP 6.2.12:

Fix to execution engine exception when loading InAppUI in Xamarin.

https://github.com/microsoft/dotnet/blob/main/releases/UWP/net-native2.2/README.md

arni
  • 2,152
  • 19
  • 14