Bug 36255 - System.TypeInitializationException when using linker option "Link framework SDKs only"
Summary: System.TypeInitializationException when using linker option "Link framework S...
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.2
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: C6SR1
Assignee: Sebastien Pouliot
: 36177 ()
Depends on:
Reported: 2015-11-25 13:43 UTC by Jon Goldberger [MSFT]
Modified: 2016-03-10 22:03 UTC (History)
8 users (show)

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

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 Jon Goldberger [MSFT] 2015-11-25 13:43:17 UTC
Created attachment 13997 [details]
Test Project

## Description

Exception with stack trace pasted below thrown when building with "Link framework SDKs only" when using CouchBase.Lite and getting an instance of the Database. Downgrading to XI resolves the issue. 

# Steps to reproduce

1. Open the attached test project in Xamarin Studio (Mac)

2. Ensure that linker option is set to "Link framework SDKs only"

3. Deploy app to device or simulator.

Expected behavior: No exception thrown. 

Actual behavior: System.TypeInitializationException is thrown (it is caught and written to the console) with the following stack trace

>System.TypeInitializationException: The type initializer for 'Couchbase.Lite.Util.Log' threw an exception. ---> System.NotSupportedException: Linked Away
>  at System.Security.Permissions.SecurityPermission..ctor (SecurityPermissionFlag ) [0x00005] in <filename unknown>:0 
>  at System.Diagnostics.Trace.get_Listeners () [0x00000] in /Users/builder/data/lanes/2356/3c0ec355/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/System/compmod/system/diagnostics/Trace.cs:34 
>  at Couchbase.Lite.Util.CustomLogger..ctor (SourceLevels logLevel) [0x00024] in /Users/borrrden/Development/couchbase-lite-net/src/Couchbase.Lite.Shared/Util/CustomLogger.cs:67 
>  at Couchbase.Lite.Util.LoggerFactory.CreateLogger () [0x00000] in /Users/borrrden/Development/couchbase-lite-net/src/Couchbase.Lite.Shared/Util/LoggerFactory.cs:57 
>  at Couchbase.Lite.Util.Log..cctor () [0x00000] in /Users/borrrden/Development/couchbase-lite-net/src/Couchbase.Lite.Shared/Util/Log.cs:55 
>  --- End of inner exception stack trace ---
>  at Couchbase.Lite.Manager..ctor (System.IO.DirectoryInfo directoryFile, Couchbase.Lite.ManagerOptions options) [0x00006] in /Users/borrrden/Development/couchbase-lite-net/src/Couchbase.Lite.Shared/Manager.cs:199 
>  at Couchbase.Lite.Manager.get_SharedInstance () [0x00000] in /Users/borrrden/Development/couchbase-lite-net/src/Couchbase.Lite.Shared/Manager.cs:111 
>  at cbtest.AppDelegate.FinishedLaunching (UIKit.UIApplication application, Foundation.NSDictionary launchOptions) [0x00003] in /Users/apple/Downloads/cbtest/cbtest/AppDelegate.cs:27 

# Supplemental info (logs, images, videos)
Diagnostic build output attached.

# Test environment (full version information)
Xamarin Studio
Version 5.10 (build 871)
Installation UUID: 85e6ee18-f90f-4874-b31d-d68d4013265e
Mono 4.2.1 (explicit/6dd2d0d)
GTK+ 2.24.23 (Raleigh theme)

Package version: 402010102

Not Installed

Apple Developer Tools
Xcode 7.1.1 (9081)
Build 7B1005

Version: (Business Edition)
Hash: 3c0ec35
Branch: master
Build date: 2015-11-12 13:05:39-0500

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

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

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

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

Version: (Starter Edition)

Build Information
Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

Operating System
Mac OS X 10.11.1
Darwin iMac.local 15.0.0 Darwin Kernel Version 15.0.0
Sat Sep 19 15:53:46 PDT 2015
root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 4 Sebastien Pouliot 2015-11-25 14:49:30 UTC
*** Bug 36177 has been marked as a duplicate of this bug. ***
Comment 5 Sebastien Pouliot 2015-11-25 14:53:09 UTC
The linker did not change but the BCL (mono) did - and include the code that triggers this.

* Add `-nolinkaway` to the "Additional mtouch arguments" of the project's option

* Larger application size (after linking)
Comment 6 Sebastien Pouliot 2015-11-26 09:17:06 UTC
referencesource/mono f0ae129d08235076ec8d03669c1ad551dc9a248d
referencesource/mono-4.3.1-branch fd2f9674528738707407606deb8a1dd2204842a0
mono/mono-4.3.1-branch 6bcd2dcbb0b8535aeded4305dbb7585ebc3967e9
maccore/master 8351e0e6fbbf041c8fb5a4f499542907984fb3b2
Comment 7 tuan_pham 2015-12-03 14:37:12 UTC
Is the fix for the issue available on the latest Xamarin Stable Channel?  I'm using Xamarin.iOS and Xamarin Studio (Build 3) and I'm still seeing this isssue.
Comment 8 Sebastien Pouliot 2015-12-03 14:46:20 UTC
@Tuan, no the fix requires several changes across repos and will require more testing before reaching stable. The workaround should (from comment #5) should work for you with only a small impact on the final code size.
Comment 9 tuan_pham 2015-12-10 16:34:17 UTC
Any ETA or estimate timeframe on when the fix will be available in stable?  Using "Don't Link" actually increased on app size quite bit (20+ MB) and will definitely exceed the 100 MB cellular download limit.
Comment 10 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-10 16:36:35 UTC
@tuan, are you using "Don't Link" or just adding `-nolinkaway` under the "Additional mtouch argument"?

Those 2 options are different. As I understand it, `-nolinkaway` only changes a small subset of `mscorlib.dll`. "Don't Link" turns off linking for _all_ assemblies.
Comment 11 tuan_pham 2015-12-10 16:45:39 UTC
I'm using "Don't Link" as the linking behavior.  I will try using the mtouch argument solution.
Comment 12 tuan_pham 2015-12-10 18:04:32 UTC
@Brendan Zagaeski thanks for pointing out the difference.  Using the mtouch argument "-nolinkaway" only added a couple MB to the app size.