Bug 39967 - Non-debug Xamarin.Forms iOS build crashes on App Start
Summary: Non-debug Xamarin.Forms iOS build crashes on App Start
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 9.6 (iOS 9.3)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-03-29 13:37 UTC by martink
Modified: 2016-04-21 13:34 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Xcode Device log with crash details (46.02 KB, text/plain)
2016-03-29 13:37 UTC, martink
Working build [Ad-hoc with Debug Enabled] (907.42 KB, text/plain)
2016-04-08 08:51 UTC, martink
Broken build [Ad-hoc with Debug Disabled] (907.45 KB, text/plain)
2016-04-08 08:51 UTC, martink
Ad-hoc with Debug Disabled (918.99 KB, text/plain)
2016-04-12 13:45 UTC, martink
Xamarin Studio iOS Device Log (10.23 KB, text/plain)
2016-04-12 15:19 UTC, martink

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description martink 2016-03-29 13:37:25 UTC
Created attachment 15542 [details]
Xcode Device log with crash details


Non-debug iOS build of Xamarin.Forms app crashes on app start-up.

Both Ad-Hoc and Release builds crash UNLESS I Enable Debugging in the Project Options > Build > iOS Debug menu.

Issue started after previous Xamarin Studio update.

See attached Device Log and Xamarin Studio details.
Comment 1 martink 2016-03-29 13:38:13 UTC
Xamarin Studio Details:

=== Xamarin Studio ===

Version 5.10.3 (build 27)
Installation UUID: afeaddc5-3486-4773-9877-a4d9f13a014c
	Mono 4.2.3 (explicit/832de4b)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402030004

=== Xamarin.Profiler ===

Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/martin/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin Inspector ===

Hash: 00c6588
Branch: master
Build date: Tue Feb 16 21:30:06 UTC 2016

=== Apple Developer Tools ===

Xcode 7.2.1 (9548.1)
Build 7C1002

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: d7cac50
Branch: master
Build date: 2016-03-21 20:13:04-0400

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 510030027
Git revision: 8dc6bca63f5cd93719a093973e74de6999864193
Build date: 2016-03-17 17:13:33-04
Xamarin addins: 45239909442742bdee83a0c0f77eecb8a08bedfa
Build lane: monodevelop-lion-cycle6-c6sr2

=== Operating System ===

Mac OS X 10.11.4
Darwin Martins-MacBook-Pro.local 15.4.0 Darwin Kernel Version 15.4.0
    Fri Feb 26 22:08:05 PST 2016
    root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64
Comment 2 Sebastien Pouliot 2016-04-07 14:23:52 UTC
Minimally we'll need to see your full build log (with `-v -v -v -v` to get all details) for both your release (non working) and debug (working) builds.

Please do a "clean" before rebuilding them so the logs will include all the steps. Thanks!
Comment 3 martink 2016-04-08 08:51:10 UTC
Created attachment 15649 [details]
Working build [Ad-hoc with Debug Enabled]
Comment 4 martink 2016-04-08 08:51:56 UTC
Created attachment 15650 [details]
Broken build [Ad-hoc with Debug Disabled]
Comment 5 Sebastien Pouliot 2016-04-12 13:17:16 UTC
Hello Martin,

There seems to be a mistake in your attachments. In both log files I can see that `Debug: True` (form msvbuild) and that `mtouch` is called with a `--debug` argument.

	Target _CompileToNative:
		MTouch Task
		  AppBundleDir: bin/iPhone/Ad-Hoc/<redacted>MobileiOS.app
		  AppManifest: bin/iPhone/Ad-Hoc/<redacted>MobileiOS.app/Info.plist
		  Architectures: ARMv7, ARM64
		  CompiledEntitlements: obj/iPhone/Ad-Hoc/Entitlements.xcent
		  Debug: True

Either the 2nd log file is incorrect or the difference between your builds is not related to the debug setting.
Comment 6 martink 2016-04-12 13:45:05 UTC
Created attachment 15680 [details]
Ad-hoc with Debug Disabled

Sorry about that; this is the correct Build Output log for the Debug Disabled
Comment 7 Sebastien Pouliot 2016-04-12 14:36:58 UTC
In general such issues are often related different linker settings (your crash report hints at something missing).

However nothing stands out in the logs. It could be something else (e.g. codegen) but that has not changed since 9.4.0 so that would be very unlucky...

Back to your original crash report we can see:

7   <redacted>              	0x0000000101cb0538 print_exception(MonoObject*, bool, NSMutableString*) (runtime.m:782)

That means the mono runtime has written additional details in the device console logs (can be accessed by XS or Xcode). Can you run this again and attach the device logs ? Thanks!
Comment 8 martink 2016-04-12 15:01:31 UTC
Would that be in the Application Output window in Xamarin Studio? It is empty :|
Comment 9 Sebastien Pouliot 2016-04-12 15:10:45 UTC
No, the device logs can be access from Xamarin Studio by

- click on View menu;
- click on Pads sub-menu;
- click on iOS Device Log item;

A new pad will be shown, with a combo box for devices. Select the one you want to execute from and then click on connect.

An alternative is to use Xcode:
- click on Windows menu
- click on Devices item;
- select the device (left);
- click on the small square+triangle icon (status bar)

In both cases you'll see the device logs, which includes a lot more information than what's being produced by your application.
Comment 10 martink 2016-04-12 15:19:15 UTC
Created attachment 15682 [details]
Xamarin Studio iOS Device Log

TIL how to view iOS Device Logs in XS!

To double-check, I used the same build config, and just Enabled Debug and the app ran without crashing
Comment 11 Sebastien Pouliot 2016-04-12 18:11:10 UTC
Unhandled Exception:
System.TypeInitializationException: The type initializer for '<redacted>.App' threw an exception. ---> System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it.

^ is there any reason (e.g. conditional compilation) that could make this call be skipped ?

or maybe it's some extra XF code that is called before the Init call ? the rest of the stacktrace makes it looks it happens very early in the application life.

at Xamarin.Forms.Device.get_PlatformServices () <0x100b579c0 + 0x0005c> in <filename unknown>:0
at Xamarin.Forms.Device.GetNamedSize (NamedSize size, System.Type targetElementType, Boolean useOldSizes) <0x100b57c30 + 0x00017> in <filename unknown>:0
at Xamarin.Forms.Device.GetNamedSize (NamedSize size, System.Type targetElementType) <0x100b57c00 + 0x0001f> in <filename unknown>:0
at <redacted>.App.SendStart () <0x1000ab910 + 0x005eb> in <filename unknown>:0
--- End of inner exception stack trace ---
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
Comment 12 martink 2016-04-17 07:52:40 UTC
global::Xamarin.Forms.Forms.Init (); is the first line of code in AppDelegate.FinishedLaunching(). No other application code gets executed before that.

No conditional compilation to skip this call.

Only happens if Debug Enabled is not checked.
Comment 13 Rolf Bjarne Kvinge [MSFT] 2016-04-18 11:39:12 UTC
(In reply to martin from comment #12)
> global::Xamarin.Forms.Forms.Init (); is the first line of code in
> AppDelegate.FinishedLaunching(). No other application code gets executed
> before that.

Type initializers can be executed at any point (as long as it happens before the type is used).

In this case the type initializer for <redacted>.App is executed before AppDelegate.FinishedLaunching, and that type initializer calls Xamarin.Forms code that require Xamarin.Forms.Init to have been called, thus the exception.

You need to change your type initializer to either call Xamarin.Forms.Init, or not do anything in any type initializer that require Xamarin.Forms.Init to have been called.
Comment 14 martink 2016-04-18 12:17:35 UTC
When the object's type initializer is executed, the object's constructor is executed first, correct?
Comment 15 Rolf Bjarne Kvinge [MSFT] 2016-04-18 12:19:10 UTC
A type initializer/constructor will always execute before an instance constructor.
Comment 16 martink 2016-04-18 13:47:06 UTC
So I am unable to find where the Type initializer for App is called in the (non-debug) program execution. I can give you access to the repo on Bitbucket?
Comment 18 martink 2016-04-18 14:16:08 UTC
Thanks, have added you.
Comment 19 martink 2016-04-20 09:00:42 UTC
So we need to do an App Store build by 29/04/2016. I preferably don't want to do it with Debug Enabled.

Any ETA on this issue?
Comment 20 Sebastien Pouliot 2016-04-20 20:58:06 UTC
I did a Release build from the head [1] of your master branch and the application starts without a crash. That's using our `cycle7` (XI 9.8), currently in the beta channel.

I'll do another build tonight after switching to `cycle6` (our current stable) tree.

[1] revision aee97e29f7ffa8751f189f799bc0cedfc527975d

The only other difference is that I'm using Xcode 7.3 (not 7.2) but that should not cause an issue like this one.
Comment 21 Sebastien Pouliot 2016-04-21 01:33:38 UTC
A release build made with (what's presently in the stable channel) also works. The application starts, without crashing, and I see "swipe for more" and two buttons "Create Account" and "Sign In". Also tried with AdHoc configuration, with same positive results.

Either you have:
(a) fixed this earlier;
(b) something that's not committed;
(c) a weird issue because Xcode 7.2 was used; or
(d) something else misconfigured/corrupted on your Mac
Comment 22 martink 2016-04-21 05:43:37 UTC

I just reproduced this with the following params:
1 - edge branch
2 - Release build to physical iPhone 6 running iOS 9.3.1
3 - double-check that Debug Enabled NOT checked
4 - app crashed on startup
Comment 23 martink 2016-04-21 05:44:29 UTC
To confirm, I am using Xcode 7.3
Comment 24 Sebastien Pouliot 2016-04-21 13:34:02 UTC
I can duplicate with your branch. It looks like you're running into a race. 

Debug executable are slower (and bigger) so Init has completed before you get the suspended call back called, which calls into your code. It works because Init was called.

OTOH for release the execution timing differs and you run this code (subclass)

(from device log)
at OurHood.Mobile.App.SendStart () <0x6ff28 + 0x007c3> in <filename unknown>:0 

called before the Xamarin.Forms.Forms.Init method is called from FinishedLaunching

(from crash report)
21  UIKit                         	0x296ef8ff -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 395

I moved the initialization to

	static AppDelegate ()
		global::Xamarin.Forms.Forms.Init ();

and it works again (it's forcing the execution of Init to happen earlier).

That might not be a perfect fix (not sure about everything Init does) so you might want to ask on forums how people deal with this kind of situation.