Thursday, May 31, 2012

MonoTouch debugging tips

This is mostly for myself so these ideas aren't lost in bugzilla comments or GMail's bottomless mail pit.

How to create a crash report when the app is hung


There is a much easier way to do this. All iOS versions and devices include a way to force quit an application:

  • Hold down the On/Off button until "slide to power off" appears.
  • Release the On/Off button.
  • Hold down the Home button.
  • After a few seconds the app will be terminated, and a crash report will be generated (the app's exception code will be 0xdeadfa11).

This snippet will cause the app to generate a crash report after 30 seconds (feel free to modify the timeout to ensure it only crashes after you've managed to hang the app).

Once the app has crashed, you should get a crash report in Xcode, hopefully with stack traces giving some clues as to what the app was trying to do when it hung. If you can't make heads and tails of the crash report, you should file a bug (remember to attach the crash report!).

Just replace the Application class in Main.cs with this (if you've made modifications to the Main.cs file yourself, you'll have to modify this code accordingly).

public class Application {
    [System.Runtime.InteropServices.DllImport ("libc")]
    static extern int strlen (IntPtr zero);
    static unsafe void Tick ()
        int zero = 0;
        int* z = &zero;
        // This is so that strlen is initialized
        // (so no/fewer locks are required later)
        strlen ((IntPtr) z); 
        // The sleep interval can be modified to sleep
        // until you're sure the app has hung.
        System.Threading.Thread.Sleep (30000);
        // Create a crash report
        strlen (IntPtr.Zero);

    static void Main (string[] args)
        new System.Threading.Thread (Tick).Start ();
        UIApplication.Main (args, null, "AppDelegate");

Next tip will go here next time I remember something I don't want to forget again

No comments:

Post a Comment