Access violation crash on Windows Phone

Last weekend Glenn and I were doing some last minute changes on a Windows Phone app for a private event. The client wanted an extra page with pivot to have an alternate view on some of the data. ‘No big deal' I thought, but development would be easy if not for unforeseen errors that are incredibly hard to hunt down (and WinRT has its fair share of those). Everything looked done and I was ready to package and publish, but to be sure I ran the app a final time and clicked around like a madman. And of course … the app crashed. Not a single exception, the debugger didn’t break, the app just closed. The output window gave us some extra info:

The program 'xx' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Not much help, so time to hit our favorite search engine and try everything we find. Most hits on the web (StackOverflow, MSDN forums) mentioned using the Dispatcher for your navigation. Bogdan Bujdea even listed another 2 possible reasons: running Visual Studio as an administrator and removing a WebView control.

If that would have solved it, you wouldn’t be reading this post. So why was our app different? First of all a small overview of the parts in the app that are important for the crash:

  • The main page is a panorama with custom background image. Items in a ListView link to the next page.
  • The second page is a pivot page with a ListView on each pivot, and each pivot has its own background color.
  • On the third level is another pivot page with again ListViews, and we get here by tapping on items on the previous page.

Important to note here is that we have two pivot pages and depending on which item is tapped on the previous page, we navigate to one of the available pivots. To enable this, we did bind the SelectedIndex property of the Pivot.

<Pivot SelectedIndex="{Binding SelectedFloor, Mode=TwoWay}">

When did our app crash? At random, but:

  • Mostly on the second page, sometimes on the third.
  • When it crashed on the third page, it was while navigating to the page. We tried to use the Dispatcher trick above, with no luck.
  • On the second page we got crashes while navigating back from the third, but also when switching between pivots.

At one point we even threw everything in Dispatcher calls, no luck of course (what did we even think haha). So I started in the code of App.xaml.cs with the idea to dig through every single line of code in the hope to hunt down the bug. Luckily I spotted this line quite fast:

// TODO: change this value to a cache size that is appropriate for your application
rootFrame.CacheSize = 1;

I tried to change it, and it worked!

rootFrame.CacheSize = 5; // equals the amount of hours wasted on Access Violation

Due to elimination we’re quite confident that the issue was the binding of the SelectedIndex property. Increasing the cache size, prevented pages from being recreated. But if someone finds a real fix, don’t hesitate to let us know.

Conclusion: last minute changes are never a good idea!

Licensed under CC BY-NC-SA 4.0; code samples licensed under MIT.
comments powered by Disqus
Built with Hugo - Based on Theme Stack designed by Jimmy