What's causing my WP7 app to crash?

巧了我就是萌 提交于 2019-12-14 00:40:50

问题


I've had a few unexplained crashes happening on both the emulator and the phone itself. Basically when my app crashes I get no dialog box whatsoever and the phone returns to the home screen.

I have the following code to display a MessageBox but this is somehow being bypassed...

// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
    MessageBox.Show(e.Exception.ToString());
}

// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.ExceptionObject.ToString());
}

The thought occurred to me that it might be related to memory, since my app deals with a lot of images. But I figure that would still be caught by my unhandled exception code above. Any ideas on how I should track this down would be appreciated.


回答1:


Keep an eye on your memory usage. An OutOfMemoryException crashes your app without calling the Application_UnhandledException handler. You can check the current memory usage with some built in methods. I blogged about this a while ago http://kodierer.blogspot.com/2010/09/windows-phone-memory-constraints.html

Here's the basic code you should add:

var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(2)};
timer.Tick += (s, e) =>
{
   var memuse = (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");
   var maxmem = (long)DeviceExtendedProperties.GetValue("DeviceTotalMemory");
   memuse /= 1024 * 1024;
   maxmem /= 1024 * 1024;
   MyTextBlock.Text = String.Format("Mem usage: {0} / {1} MB", memuse, maxmem);
};
timer.Start();



回答2:


A few things which have happened to me:

  • If you're doing things on other threads, then IIRC exceptions on those threads will cause the app to just terminate. You may want to wrap the new thread code in an exception handler which propagates the exception to the UI thread
  • If your app throws an exception before the first page is loaded, that can cause the app to just die without the appropriate handler being called
  • If you've got a StackOverflowException, that can't be caught and will just make the app bomb

You may want to add some debug-build-only persistent logging (loaded and displayed within the app itself) to make it easier to work out how far the previous run of the application had got before crashing.




回答3:


My app crashed in exactly the same way.

I tracked it down to throwing an OutOfMemoryException inside a DispatcherTimer tick handler, though the problem probably occurs elsewhere as well.

However, it is not the case that an OutOfMemoryException always takes down your program. It does not. I tried it in various other handlers, and it was correctly caught.

I've posted a blog entry about lost exceptions here




回答4:


Could your app be being watchdoged for being unresponsive for too long? Perhaps due to the load time of lots of images and this code being executed on the UI thread.



来源:https://stackoverflow.com/questions/4239193/whats-causing-my-wp7-app-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!