Bug 28823

Summary: strange behaviour causing loaded classes to appear unloaded when referenced from class attributes and using nay kind of linking.
Product: iOS Reporter: George Cook <george.cook>
Component: ToolsAssignee: Sebastien Pouliot <sebastien>
Status: RESOLVED FIXED    
Severity: normal CC: bugzilla.xamarin, david, mono-bugs+monotouch, mono-bugs+mono, sebastien, udhams
Priority: Normal    
Version: master   
Target Milestone: 8.13 (C6 alpha)   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: example project showing the issue
image showing that the app crashes and that the LinkerIssuses.iOS class IS referencing sharedClasses.
image showing linker options for project, which crashes upon encountering JsonConverter tags
image showing references for iOS project, demonstrating that the sharedClasses project is referenced
provided workaround does NOT work. Class is loaded. The diagnosis given is inaccurate. Look insideLinkerIssue.cs

Description George Cook 2015-04-06 17:45:37 UTC
I use Newtonsoft.Json (i.e. json.net) in my project. I have lots of Code that uses the [JsonConverter] attribute to inform the json deserializers which classes to use.

Everything works fine, until I turn on any linking in my project (i.e. even SDK linking is enough to break my project).

what happens is that the moment I try to deserialize nay class with the JsonConverter attribute I get a runtime exception saying the class could not be loaded. The strange thing is that the class IS loaded. I verify this in the following way.

		1 var jsonString = "{\"items\":[]}";
		2	var settings = new JsonSerializerSettings ();
		3		settings.Converters.Add (new FixBadEmptyJsonArrayConverter<string> ());
		4	var f = new FixBadEmptyJsonArrayConverter<string> ();
		5	var testObject = JsonConvert.DeserializeObject<object> (jsonString, settings);

		6			ProfileDTO profileDTO = await ProfileService.LoadProfileWithAccount (loadedAccount);

I put a breakpoint at line 6 and inside my class . the code in FixBadEmptyJsonArrayConverter is executed (i.e breakpoint hit). testObject is correctly deserialized. var f is initialized. THIS CLASS IS 100% loaded.

I then continue execution and upon running line 6, I get:
> Could not load type Giganet.Core.Model.Converters.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], GiganetMobile.SharedModels, Version=1.0.5574.29870, Culture=neutral, PublicKeyToken=null while decoding custom attribute

This is a serious issue as I can't possibly use the linker.

My app is 120mb in size, 6 times larger than it was when it was native only. I NEED this to work. I've been talking about this extensively on the forum, and in the course of identifying the issue raised another issue about the [Preserve] attrinbute. however, I can now see this has nothing to do with preserving at all. THE CLASS IS LOADED.

I don't know what's going on, or how to work arount it; but this is not a good situation at all.
Comment 1 George Cook 2015-04-06 17:49:23 UTC
addition, the offending line of code is in one of my dtos and is this.


namespace Giganet.Accounts.Login.Model
{
	public class ProfileDTO : WrappedData<ProfileDTO>
	{
		
		[JsonProperty ("user")]
		public Account Account { get; set; }

		//TODO - server sends {} when there's no items
		[JsonProperty ("favouriteMedia")]
		[JsonConverter (typeof(FixBadEmptyJsonArrayConverter<string[]>))]
		public string[] FavouriteIds;


The jsconconverter property there is what crashes at runtime.
Comment 2 George Cook 2015-04-06 18:35:20 UTC
Created attachment 10647 [details]
example project showing the issue

this project doesn't even need linker flags set. it just blows up on the deserialization.
Comment 3 Marek Safar 2015-04-07 17:15:17 UTC
This is a build dependency issue. Adding explicit reference of SharedClasses to LinkerIssue.iOS project fixes the problem.
Comment 4 George Cook 2015-04-07 17:21:03 UTC
Perhaps I'm missing something. you will see from the image I'm about to upload. you will see I have a reference to SharedClasses in LinkerIssue.iOS.

Does "explicit" reference mean something else in Xamarin studios; becuase you will observe from the screen shot the reference is there, and the program has crashed. 

Linker flags are set to "link sdk only"

thanks
Comment 5 George Cook 2015-04-07 17:21:41 UTC
Created attachment 10663 [details]
image showing that the app crashes and that the LinkerIssuses.iOS class IS referencing sharedClasses.
Comment 6 George Cook 2015-04-07 17:23:48 UTC
Created attachment 10664 [details]
image showing linker options for project, which crashes upon encountering JsonConverter tags
Comment 7 George Cook 2015-04-07 17:25:19 UTC
Created attachment 10665 [details]
image showing references for iOS project, demonstrating that the sharedClasses project is referenced
Comment 8 Sebastien Pouliot 2015-04-07 22:04:24 UTC
I think that what Marek meant by "explicit" is that there's no "real" (in the sense that the C# compiler can't remove) references to SharedClasses.dll from LinkerIssue.iOS. That's a common cause but I do not think that's the issue here.

Can you tell me all the version information* about the software you're using ?

* The easiest way to get exact version information is to use the "Xamarin Studio" menu, "About Xamarin Studio" item, "Show Details" button and copy/paste the version informations (you can use the "Copy Information" button).
Comment 9 Sebastien Pouliot 2015-04-07 22:47:39 UTC
It's a different but similar issue. The exact reasons for this are quite complex.

1. Little known fact: the assembly references you give to Xamarin Studio (or VS) are hints. It tells the compiler where to look for types - but if it's not needed then it won't be part of the assembly binary [1]. That's totally normal (not a bug) from the C# compiler perspective;

2. Our tools uses assembly references to find what's required at runtime. IOW if there's a reference then we copy the assemblies into the .app bundle. That step occurs well before the linker is even invoked (or even if it's disabled);

3. That works very well in a 99%+ of the cases. The minority exception is when a type is loaded dynamically, e.g. `Type.GetType (xyz)`. In such case there's no assembly reference (by default) and you need to create one (even artificially) to get the assembly copied in your .app bundle.

4. Now there's a very subtle case (the one you just hit, call it the 0.0001%, AFAIK you're the first one to report it [2]) with custom attributes. Custom attributes uses a special encoding and when used with `System.Type` argument(s) they do _not_ add the assembly into the assemblies references (it's only encoded in the custom attribute, no where else in the metadata). This, along with the compiler not including unneeded references (#1), means your LinkerIssue.dll does not have an _explicit_/real/hard reference to SharedClasses.dll - even if (from the IDE perperctive) it looks like one should be there.

This is why it does not get copied to your app bundle - and that cause the exception you've been seeing.


Now the solution is much more simple (than the above explication).

You need to create a explicit (real) reference between your code and the SharedClasses.dll assembly, one that the C# compiler cannot ignore. That can be very simple, e.g. add this line to your `Main` method:

> 			typeof(SharedClasses.JsonCreationConverter<object>).ToString ();

That will ensure the C# compiler keeps the reference to SharedClasses.dll. In turn our tools (mtouch's resolver) will see that this assembly is required and will make sure it's copied into your .app bundle.


[1] that's actually a very good thing as it makes it easy to create smaller applications (even with the linker disabled). Without this behaviour the whole BCL would need to be copied into each application (and the executable size would be much bigger than what Apple allows on the AppStore for native executables).

[2] if there was a bit more code in that shared assembly then it would likely be used (and referenced) elsewhere in the application. IOW the condition is not rare - it's just that there's generally something else that will create that hard reference (and no one notice that subtle case).
Comment 10 George Cook 2015-04-07 23:48:55 UTC
I appreciate the long explanation; but it just confuses me further. in my real project, I reference *many* classes from (my equivalent of) the sharedModels project, the dll is most certainly loaded.

Furthermore, the converter class is loaded in my real project at runtime, as I can instantiate it, and I can deserialize json using it, and breakpoint inside of that class.

the problem *only* occurs with the use of the [JsonConverter] attribute.

I just added the line to my main.cs, and ran the project again. I get the exact same behaviour.

the class is loaded. I can instantiate it. I can breakpoint debug code INSIDE the class.

I have no idea what the hell is happening here, and I really do appreciate your explanation; but that is not what is going on. 

The code you asked me to add doesn't work. Did you try it? If so, can you please show me how? I placed the code in both the iOS project and the LinkerIssue project to no avail.

I believe that this is something else, and it's potentially a real problem, and there are others on the forum talking about this; but I don't think they've been around the block enough to know that simply turning off linking to reduce size is going to bite them in the arse pretty badly in the not so distant future. 

It's going to blow up in their faces for sure and leave a very bad taste if their organizations are experimenting with Xamarin and find at the last minute that their only option is to release >130mb binaries in place of 40mb ones. I for one have to put my organization's Xamarin exploration on hold until I resolve this, or it will be dead in the water : they wont' go near it with these absurd app sizes. At the very least, I can tell them that the community and the Xamarin team are very responsive; but it won't be enough for me to present this to anyone - and I dread to think what would happen if I was working on a real project and this occurred - thankfully, this is just a technical poc.

I will attach a zip of the project in a moment, which shows:

I've added your code, 
it doens't resolve the issue,
the class is loaded, and deserializes fine if I do not use the attribute. 
the app will crash upon attempting to reflect the [JsonConverter] attribute.

I hope that will be sufficient for you guys to work out what this is.
Comment 11 George Cook 2015-04-07 23:51:32 UTC
Created attachment 10668 [details]
provided workaround does NOT work. Class is loaded. The diagnosis given is inaccurate. Look insideLinkerIssue.cs

Look insideLinkerIssue.cs, all will be abundantly clear.
Comment 12 Sebastien Pouliot 2015-04-08 08:33:58 UTC
> Did you try it?

Of course :) It's the only change I did to your test case last night. With it:

* the TypeLoadException was gone afterward; and

* the required SharedClasses.dll was included in the .app bundle (and it's missing before that for the reasons given above);

There might be another issue, specific to the version of the software you're using. Please provide the information requested in comment #8.

Also please be more specific, e.g.

> Class is loaded.

can be misinterpreted, which exact type are you talking about.
Comment 13 Sebastien Pouliot 2015-04-08 08:39:41 UTC
If I run you last attachment I get a black screen (but no exception) and the following is printed in the "Application Output" of XS.

Starting iPhone 5s
Launching application
Application launched. PID = 36755
Press enter to terminate the application
>Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/Xamarin.iOS.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/System.dll
Thread started:  #2
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/LinkerIssue.iOS.exe
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/Xamarin.Forms.Platform.iOS.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/Xamarin.Forms.Core.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/SharedClasses.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/Newtonsoft.Json.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/System.Xml.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/System.Runtime.Serialization.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/System.Core.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/Mono.Dynamic.Interpreter.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/System.Net.Http.dll
Loaded assembly: /Users/poupou/Library/Developer/CoreSimulator/Devices/05151A45-B795-4381-B95B-5468FF6442AD/data/Containers/Bundle/Application/4DB40E38-4815-4344-B090-27A3514C3F31/LinkerIssue.iOS.app/LinkerIssue.dll
Thread started:  #3
Thread started:  #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
completed {
  "items": []
}
2015-04-08 08:35:11.982 LinkerIssue.iOS[36755:31542546] completed {
  "items": []
}

Should I expect something else ?
Comment 14 Sebastien Pouliot 2015-04-08 08:43:36 UTC
If I uncomment the line

			//var dummy = typeof(SharedClasses.JsonCreationConverter<object>).ToString ();

then I get the exception again.


2015-04-08 08:37:46.520 LinkerIssue.iOS[36804:31556514] Unhandled managed exception:
Could not load type SharedClasses.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], SharedClasses, Version=1.0.5576.15438, Culture=neutral, PublicKeyToken=null while decoding custom attribute (System.TypeLoadException)
  at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool)
  at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType, Boolean inheritedOnly) [0x00019] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:103 
  at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00040] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:152 
  at System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, System.Type type, Boolean inherit) [0x00016] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/Attribute.cs:232 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes (System.Object attributeProvider, System.Type attributeType, Boolean inherit) [0x0007f] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes[JsonConverterAttribute] (System.Object attributeProvider, Boolean inherit) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttribute[JsonConverterAttribute] (System.Object attributeProvider, Boolean inherit) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonConverterAttribute] (System.Reflection.MemberInfo memberInfo) [0x0002c] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonConverterAttribute] (System.Object provider) [0x0001b] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Object,Newtonsoft.Json.JsonConverterAttribute].AddValue (System.Object key) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Object,Newtonsoft.Json.JsonConverterAttribute].Get (System.Object key) [0x00010] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.CachedAttributeGetter`1[Newtonsoft.Json.JsonConverterAttribute].GetAttribute (System.Object type) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetCachedAttribute[JsonConverterAttribute] (System.Object attributeProvider) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter (System.Object attributeProvider) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes (Newtonsoft.Json.Serialization.JsonProperty property, System.Object attributeProvider, System.String name, System.Type declaringType, MemberSerialization memberSerialization, System.Boolean& allowNonPublicAccess) [0x00157] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty (System.Reflection.MemberInfo member, MemberSerialization memberSerialization) [0x00046] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties (System.Type type, MemberSerialization memberSerialization) [0x0002f] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract (System.Type objectType) [0x00035] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x000bf] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00038] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00005] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x0000e] in <filename unknown>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00057] in <filename unknown>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[BadArrayJsonTest] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[BadArrayJsonTest] (System.String value) [0x00000] in <filename unknown>:0 
  at LinkerIssue.App.I_WILL_CRASH_CLAIMING_THAT_THE_CLASS_IS_NOT_LOADED_BUT_YOU_SAW_THAT_IT_WAS () [0x00008] in /Users/poupou/Downloads/LinkerIssue-2/LinkerIssue/LinkerIssue.cs:47 
  at LinkerIssue.App..ctor () [0x0001e] in /Users/poupou/Downloads/LinkerIssue-2/LinkerIssue/LinkerIssue.cs:17 
  at LinkerIssue.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x00007] in /Users/poupou/Downloads/LinkerIssue-2/iOS/AppDelegate.cs:17 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/poupou/git/xamarin/maccore/src/UIKit/UIApplication.cs:63 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/poupou/git/xamarin/maccore/src/UIKit/UIApplication.cs:47 
  at LinkerIssue.iOS.Application.Main (System.String[] args) [0x00008] in /Users/poupou/Downloads/LinkerIssue-2/iOS/Main.cs:19 


Please make sure you do a Clean/Rebuild in XS and also do a "Reset Content and Settings" in the simulator. There could be a stale/bad file somewhere that gets loaded.
Comment 15 George Cook 2015-04-08 10:50:52 UTC
Ok guys, thanks for your persistence here.

I've tried this on the latest alpha stream, and also on Xamarin studio 5.8 (build 443) on mac.

I donwloaded the zip fie I gave you onto my other mac, unchanged.

I get excpetion Could not load type SharedClasses.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], SharedClasses, Version=1.0.5576.17574, Culture=neutral, PublicKeyToken=null while decoding custom attribute

at runtime, at the exact same place.

I did clean all, I reset the simulator. I can record a video to show you guys exactly what I'm doing; but to recap.

On a different machine, load the zip file from https://bugzilla.xamarin.com/show_bug.cgi?id=28823
Run xamarin studio 5.8 (build 443),
do not change anything, run - crashes at runtime with the above exception.
Reset simulator,
clean all projects in xamarin studio,
run again,
same crash as above.

In every case the crash is :

2015-04-08 09:50:14.605 LinkerIssue.iOS[1109:14198] Unhandled managed exception: Could not load type SharedClasses.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], SharedClasses, Version=1.0.5576.17574, Culture=neutral, PublicKeyToken=null while decoding custom attribute (System.TypeLoadException)
  at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool)
  at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType, Boolean inheritedOnly) [0x00019] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:103 
  at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00040] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:152 
  at System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, System.Type type, Boolean inherit) [0x00016] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Attribute.cs:232 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes (System.Object attributeProvider, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes[JsonConverterAttribute] (System.Object attributeProvider, Boolean inherit) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttribute[JsonConverterAttribute] (System.Object attributeProvider, Boolean inherit) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonConverterAttribute] (System.Reflection.MemberInfo memberInfo) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonConverterAttribute] (System.Object provider) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Object,Newtonsoft.Json.JsonConverterAttribute].AddValue (System.Object key) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Object,Newtonsoft.Json.JsonConverterAttribute].Get (System.Object key) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.CachedAttributeGetter`1[Newtonsoft.Json.JsonConverterAttribute].GetAttribute (System.Object type) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetCachedAttribute[JsonConverterAttribute] (System.Object attributeProvider) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter (System.Object attributeProvider) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes (Newtonsoft.Json.Serialization.JsonProperty property, System.Object attributeProvider, System.String name, System.Type declaringType, MemberSerialization memberSerialization, System.Boolean& allowNonPublicAccess) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty (System.Reflection.MemberInfo member, MemberSerialization memberSerialization) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties (System.Type type, MemberSerialization memberSerialization) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract (System.Type objectType) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[BadArrayJsonTest] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[BadArrayJsonTest] (System.String value) [0x00000] in <filename unknown>:0 
  at LinkerIssue.App.I_WILL_CRASH_CLAIMING_THAT_THE_CLASS_IS_NOT_LOADED_BUT_YOU_SAW_THAT_IT_WAS () [0x00008] in /Users/georgecook/Downloads/LinkerIssue/LinkerIssue/LinkerIssue.cs:47 
  at LinkerIssue.App..ctor () [0x0001e] in /Users/georgecook/Downloads/LinkerIssue/LinkerIssue/LinkerIssue.cs:17 
  at LinkerIssue.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x00007] in /Users/georgecook/Downloads/LinkerIssue/iOS/AppDelegate.cs:17 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46 
  at LinkerIssue.iOS.Application.Main (System.String[] args) [0x00018] in /Users/georgecook/Downloads/LinkerIssue/iOS/Main.cs:19 
2015-04-08 09:50:14.607 LinkerIssue.iOS[1109:14198] critical: Stacktrace:

2015-04-08 09:50:14.607 LinkerIssue.iOS[1109:14198] critical: 
Native stacktrace:

2015-04-08 09:50:14.638 LinkerIssue.iOS[1109:14198] critical: 	0   LinkerIssue.iOS                     0x0018d977 mono_handle_native_sigsegv + 311
2015-04-08 09:50:14.638 LinkerIssue.iOS[1109:14198] critical: 	1   LinkerIssue.iOS                     0x0019930a sigabrt_signal_handler + 122
2015-04-08 09:50:14.638 LinkerIssue.iOS[1109:14198] critical: 	2   libsystem_platform.dylib            0x03ef803b _sigtramp + 43
2015-04-08 09:50:14.639 LinkerIssue.iOS[1109:14198] critical: 	3   ???                                 0xffffffff 0x0 + 4294967295
2015-04-08 09:50:14.639 LinkerIssue.iOS[1109:14198] critical: 	4   libsystem_sim_c.dylib               0x03c7fa5a abort + 156
2015-04-08 09:50:14.639 LinkerIssue.iOS[1109:14198] critical: 	5   LinkerIssue.iOS                     0x0030babf xamarin_unhandled_exception_handler + 287
2015-04-08 09:50:14.639 LinkerIssue.iOS[1109:14198] critical: 	6   LinkerIssue.iOS                     0x0018e1cb mono_invoke_unhandled_exception_hook + 91
2015-04-08 09:50:14.639 LinkerIssue.iOS[1109:14198] critical: 	7   LinkerIssue.iOS                     0x0018d1dd mono_handle_exception_internal + 6685
2015-04-08 09:50:14.640 LinkerIssue.iOS[1109:14198] critical: 	8   LinkerIssue.iOS                     0x0018b7b9 mono_handle_exception + 41
2015-04-08 09:50:14.640 LinkerIssue.iOS[1109:14198] critical: 	9   LinkerIssue.iOS                     0x00126b78 mono_x86_throw_exception + 136
2015-04-08 09:50:14.640 LinkerIssue.iOS[1109:14198] critical: 	10  ???                                 0x0cba2e57 0x0 + 213528151
2015-04-08 09:50:14.640 LinkerIssue.iOS[1109:14198] critical: 	11  LinkerIssue.iOS                     0x00200d72 custom_attrs_get_by_type + 130
2015-04-08 09:50:14.640 LinkerIssue.iOS[1109:14198] critical: 	12  ???                                 0x0cbe57a8 0x0 + 213800872
2015-04-08 09:50:14.641 LinkerIssue.iOS[1109:14198] critical: 	13  ???                                 0x0cbe5614 0x0 + 213800468
2015-04-08 09:50:14.641 LinkerIssue.iOS[1109:14198] critical: 	14  ???                                 0x0cbe4658 0x0 + 213796440
2015-04-08 09:50:14.641 LinkerIssue.iOS[1109:14198] critical: 	15  ???                                 0x0eb45bd4 0x0 + 246701012
2015-04-08 09:50:14.641 LinkerIssue.iOS[1109:14198] critical: 	16  ???                                 0x0eafe630 0x0 + 246408752
2015-04-08 09:50:14.642 LinkerIssue.iOS[1109:14198] critical: 	17  ???                                 0x0eb01f68 0x0 + 246423400
2015-04-08 09:50:14.642 LinkerIssue.iOS[1109:14198] critical: 	18  ???                                 0x0eaff8a0 0x0 + 246413472
2015-04-08 09:50:14.642 LinkerIssue.iOS[1109:14198] critical: 	19  ???                                 0x0eb45954 0x0 + 246700372
2015-04-08 09:50:14.642 LinkerIssue.iOS[1109:14198] critical: 	20  ???                                 0x0eafd528 0x0 + 246404392
2015-04-08 09:50:14.642 LinkerIssue.iOS[1109:14198] critical: 	21  ???                                 0x0eafccd4 0x0 + 246402260
2015-04-08 09:50:14.643 LinkerIssue.iOS[1109:14198] critical: 	22  ???                                 0x0eafcc0c 0x0 + 246402060
2015-04-08 09:50:14.643 LinkerIssue.iOS[1109:14198] critical: 	23  ???                                 0x0eafca88 0x0 + 246401672
2015-04-08 09:50:14.643 LinkerIssue.iOS[1109:14198] critical: 	24  ???                                 0x0eafbf04 0x0 + 246398724
2015-04-08 09:50:14.643 LinkerIssue.iOS[1109:14198] critical: 	25  ???                                 0x0eb07700 0x0 + 246445824
2015-04-08 09:50:14.643 LinkerIssue.iOS[1109:14198] critical: 	26  ???                                 0x0eb44d38 0x0 + 246697272
2015-04-08 09:50:14.644 LinkerIssue.iOS[1109:14198] critical: 	27  ???                                 0x0eb43810 0x0 + 246691856
2015-04-08 09:50:14.644 LinkerIssue.iOS[1109:14198] critical: 	28  ???                                 0x0eb1f82e 0x0 + 246544430
2015-04-08 09:50:14.644 LinkerIssue.iOS[1109:14198] critical: 	29  ???                                 0x0eb04e08 0x0 + 246435336
2015-04-08 09:50:14.644 LinkerIssue.iOS[1109:14198] critical: 	30  ???                                 0x0eaf7f8f 0x0 + 246382479
2015-04-08 09:50:14.644 LinkerIssue.iOS[1109:14198] critical: 	31  ???                                 0x0eaf6eba 0x0 + 246378170
2015-04-08 09:50:14.645 LinkerIssue.iOS[1109:14198] critical: 	32  ???                                 0x0eaf6d25 0x0 + 246377765
2015-04-08 09:50:14.645 LinkerIssue.iOS[1109:14198] critical: 	33  ???                                 0x0eaf66fc 0x0 + 246376188
2015-04-08 09:50:14.645 LinkerIssue.iOS[1109:14198] critical: 	34  ???                                 0x0eaf4474 0x0 + 246367348
2015-04-08 09:50:14.645 LinkerIssue.iOS[1109:14198] critical: 	35  ???                                 0x0eaf4173 0x0 + 246366579
2015-04-08 09:50:14.645 LinkerIssue.iOS[1109:14198] critical: 	36  ???                                 0x0eaedd24 0x0 + 246340900
2015-04-08 09:50:14.646 LinkerIssue.iOS[1109:14198] critical: 	37  ???                                 0x0eaed990 0x0 + 246339984
2015-04-08 09:50:14.646 LinkerIssue.iOS[1109:14198] critical: 	38  ???                                 0x0eb41c80 0x0 + 246684800
2015-04-08 09:50:14.646 LinkerIssue.iOS[1109:14198] critical: 	39  ???                                 0x0eb41bdc 0x0 + 246684636
2015-04-08 09:50:14.646 LinkerIssue.iOS[1109:14198] critical: 	40  ???                                 0x0eab68f0 0x0 + 246114544
2015-04-08 09:50:14.646 LinkerIssue.iOS[1109:14198] critical: 	41  ???                                 0x0d93d264 0x0 + 227791460
2015-04-08 09:50:14.647 LinkerIssue.iOS[1109:14198] critical: 	42  ???                                 0x0d93d499 0x0 + 227792025
2015-04-08 09:50:14.647 LinkerIssue.iOS[1109:14198] critical: 	43  LinkerIssue.iOS                     0x001bd355 mono_jit_runtime_invoke + 725
2015-04-08 09:50:14.647 LinkerIssue.iOS[1109:14198] critical: 	44  LinkerIssue.iOS                     0x002530bf mono_runtime_invoke + 127
2015-04-08 09:50:14.647 LinkerIssue.iOS[1109:14198] critical: 	45  LinkerIssue.iOS                     0x003153bb xamarin_trampoline + 5147
2015-04-08 09:50:14.648 LinkerIssue.iOS[1109:14198] critical: 	46  UIKit                               0x00e7b97c -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 291
2015-04-08 09:50:14.648 LinkerIssue.iOS[1109:14198] critical: 	47  UIKit                               0x00e7c687 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2869
2015-04-08 09:50:14.648 LinkerIssue.iOS[1109:14198] critical: 	48  UIKit                               0x00e7fc0d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
2015-04-08 09:50:14.648 LinkerIssue.iOS[1109:14198] critical: 	49  UIKit                               0x00e987d0 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
2015-04-08 09:50:14.648 LinkerIssue.iOS[1109:14198] critical: 	50  UIKit                               0x00e7e81f -[UIApplication workspaceDidEndTransaction:] + 155
2015-04-08 09:50:14.649 LinkerIssue.iOS[1109:14198] critical: 	51  FrontBoardServices                  0x05fd59de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
2015-04-08 09:50:14.649 LinkerIssue.iOS[1109:14198] critical: 	52  FrontBoardServices                  0x05fd546f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
2015-04-08 09:50:14.649 LinkerIssue.iOS[1109:14198] critical: 	53  FrontBoardServices                  0x05fe7425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
2015-04-08 09:50:14.649 LinkerIssue.iOS[1109:14198] critical: 	54  CoreFoundation                      0x025bb1c0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
2015-04-08 09:50:14.650 LinkerIssue.iOS[1109:14198] critical: 	55  CoreFoundation                      0x025b0ad3 __CFRunLoopDoBlocks + 195
2015-04-08 09:50:14.650 LinkerIssue.iOS[1109:14198] critical: 	56  CoreFoundation                      0x025b092b __CFRunLoopRun + 2715
2015-04-08 09:50:14.650 LinkerIssue.iOS[1109:14198] critical: 	57  CoreFoundation                      0x025afbcb CFRunLoopRunSpecific + 443
2015-04-08 09:50:14.650 LinkerIssue.iOS[1109:14198] critical: 	58  CoreFoundation                      0x025af9fb CFRunLoopRunInMode + 123
2015-04-08 09:50:14.651 LinkerIssue.iOS[1109:14198] critical: 	59  UIKit                               0x00e7e1e4 -[UIApplication _run] + 571
2015-04-08 09:50:14.651 LinkerIssue.iOS[1109:14198] critical: 	60  UIKit                               0x00e818b6 UIApplicationMain + 1526
2015-04-08 09:50:14.651 LinkerIssue.iOS[1109:14198] critical: 	61  ???                                 0x0d937448 0x0 + 227767368
2015-04-08 09:50:14.651 LinkerIssue.iOS[1109:14198] critical: 	62  ???                                 0x0d937248 0x0 + 227766856
2015-04-08 09:50:14.651 LinkerIssue.iOS[1109:14198] critical: 	63  ???                                 0x0d9354d8 0x0 + 227759320
2015-04-08 09:50:14.652 LinkerIssue.iOS[1109:14198] critical: 	64  ???                                 0x0d935218 0x0 + 227758616
2015-04-08 09:50:14.652 LinkerIssue.iOS[1109:14198] critical: 	65  ???                                 0x0d935376 0x0 + 227758966
2015-04-08 09:50:14.652 LinkerIssue.iOS[1109:14198] critical: 	66  LinkerIssue.iOS                     0x001bd355 mono_jit_runtime_invoke + 725
2015-04-08 09:50:14.652 LinkerIssue.iOS[1109:14198] critical: 	67  LinkerIssue.iOS                     0x002530bf mono_runtime_invoke + 127
2015-04-08 09:50:14.652 LinkerIssue.iOS[1109:14198] critical: 	68  LinkerIssue.iOS                     0x00257c21 mono_runtime_exec_main + 401
2015-04-08 09:50:14.653 LinkerIssue.iOS[1109:14198] critical: 	69  LinkerIssue.iOS                     0x002579ca mono_runtime_run_main + 682
2015-04-08 09:50:14.653 LinkerIssue.iOS[1109:14198] critical: 	70  LinkerIssue.iOS                     0x0012206d mono_jit_exec + 93
2015-04-08 09:50:14.653 LinkerIssue.iOS[1109:14198] critical: 	71  LinkerIssue.iOS                     0x00309b24 xamarin_main + 2500
2015-04-08 09:50:14.653 LinkerIssue.iOS[1109:14198] critical: 	72  LinkerIssue.iOS                     0x000e37d5 main + 117
2015-04-08 09:50:14.654 LinkerIssue.iOS[1109:14198] critical: 	73  libdyld.dylib                       0x03bd5ac9 start + 1
2015-04-08 09:50:14.654 LinkerIssue.iOS[1109:14198] critical: 
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
============================================
Comment 16 George Cook 2015-04-08 10:53:57 UTC
and to be clear, in all cases, the first method call in the App class (in LinkerIssue.cs), named THIS_CODE_WILL_WORK_FINE_THE_CLASS_IS_LOADED works - so the class FixBadEmptyJsonArrayConverter, and it's dll absolutely must be loaded.
Comment 17 Sebastien Pouliot 2015-04-08 13:20:31 UTC
The provided information is not detailed enough, I need the exact version of the software you're using. Please provide us with the data requested in comment #8
Comment 18 George Cook 2015-04-08 13:21:25 UTC
sorry, missed that.


Xamarin Studio
Version 5.8 (build 443)
Installation UUID: a24a6e41-431a-45a1-9bde-6e41e9fd24ba
Runtime:
	Mono 3.12.1 ((detached/b7764aa)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010000

Apple Developer Tools
Xcode 6.1 (6604)
Build 6A1052d

Xamarin.Mac
Not Installed

Xamarin.Android
Version: 4.20.0.37 (Indie Edition)
Android SDK: /Users/georgecook/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.4   (API level 19)
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: Unknown version
Location: /Applications/Xamarin Android Player.app

Xamarin.iOS
Version: 8.8.0.2 (Indie Edition)
Hash: ccfcd59
Branch: 
Build date: 2015-03-10 02:20:32-0400

Build Information
Release ID: 508000443
Git revision: 73883239470cbe8e261c94d95f7c3d0452fd393b
Build date: 2015-03-10 07:22:51-04
Xamarin addins: a2ff7b617f09d9c45d8bbf3d010b5db0d7d36100

Operating System
Mac OS X 10.10.2
Darwin galadriel.local 14.1.0 Darwin Kernel Version 14.1.0
    Mon Dec 22 23:10:38 PST 2014
    root:xnu-2782.10.72~2/RELEASE_X86_64 x86_64
Comment 19 George Cook 2015-04-08 13:29:16 UTC
and I get the exact same behaviour with this version also.

Xamarin Studio
Version 5.8.2 (build 7)
Installation UUID: a24a6e41-431a-45a1-9bde-6e41e9fd24ba
Runtime:
    Mono 3.12.1 ((detached/0849ec7)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 312010003

Xamarin.Android
Version: 4.20.1.0 (Indie Edition)
Android SDK: /Users/georgecook/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        2.1   (API level 7)
        2.2   (API level 8)
        2.3   (API level 10)
        3.1   (API level 12)
        4.0.3 (API level 15)
        4.1   (API level 16)
        4.4   (API level 19)
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: Unknown version
Location: /Applications/Xamarin Android Player.app

Apple Developer Tools
Xcode 6.1 (6604)
Build 6A1052d

Xamarin.iOS
Version: 8.8.2.4 (Indie Edition)
Hash: 981acb3
Branch: 
Build date: 2015-03-25 14:21:05-0400

Xamarin.Mac
Not Installed

Build Information
Release ID: 508020007
Git revision: 067ed06873402cf780f8a0899bb332c2a1c0af22
Build date: 2015-03-27 14:17:13-04
Xamarin addins: 02510c4990dee3475c282b3182d588975a63ff07

Operating System
Mac OS X 10.10.2
Darwin galadriel.local 14.1.0 Darwin Kernel Version 14.1.0
    Mon Dec 22 23:10:38 PST 2014
    root:xnu-2782.10.72~2/RELEASE_X86_64 x86_64
Comment 20 Sebastien Pouliot 2015-04-08 14:05:23 UTC
Thanks! I got to a configuration that is closer to yours.

Starting from scratch, here's what I did, step-by-step:

1. I downloaded again the *first* test case https://bugzilla.xamarin.com/attachment.cgi?id=10647

and ran it (as is) on the simulator. I got the TypeLoadException

2015-04-08 13:45:18.929 LinkerIssue.iOS[73996:32681099] Unhandled managed exception:
Could not load type SharedClasses.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], SharedClasses, Version=1.0.5576.24752, Culture=neutral, PublicKeyToken=null while decoding custom attribute (System.TypeLoadException)

* The configuration uses "Don't link";
* LinkerIssue.iOS.exe has no reference (in XS) to SharedClasses.dll


2. I added two lines in Main.cs (before the call to UIApplicationMain)

			var assembly = System.IO.Path.GetFullPath (System.IO.Path.Combine (NSBundle.MainBundle.ExecutablePath, "..", "SharedClasses.dll"));
			Console.WriteLine ("{0} was {1}found", assembly, System.IO.File.Exists (assembly) ? String.Empty : "NOT ");

to confirm that nothing else indirectly caused SharedClasses.dll to be copied into the .app bundle. I got this (expected) output:

2015-04-08 13:47:42.234 LinkerIssue.iOS[74039:32692243] /Users/poupou/Library/Developer/CoreSimulator/Devices/1C6D73E1-2CB4-484D-A6E7-E0D57A39D3CB/data/Containers/Bundle/Application/1DD6B5E0-CA59-49A5-A624-EF6DE3DD053C/LinkerIssue.iOS.app/SharedClasses.dll was NOT found

before the TypeLoadException.


3. I added a reference to SharedClasses.dll to LinkerIssue.iOS.exe. That did not change anything, I still get:

2015-04-08 13:47:42.234 LinkerIssue.iOS[74039:32692243] /Users/poupou/Library/Developer/CoreSimulator/Devices/1C6D73E1-2CB4-484D-A6E7-E0D57A39D3CB/data/Containers/Bundle/Application/1DD6B5E0-CA59-49A5-A624-EF6DE3DD053C/LinkerIssue.iOS.app/SharedClasses.dll was NOT found

before the TypeLoadException. That's expected since the C# compiler can remove that unrequired assembly reference (and nothing was using it).


4. I added another line to Main.cs

> typeof(SharedClasses.JsonCreationConverter<object>).ToString ();

That changed the C.WL output to say the .dll was found. That was expected since the C# compiler now needs to include the reference into the assembly binary (and that means it gets copied into the .app bundle).

2015-04-08 13:52:00.231 LinkerIssue.iOS[74177:32710787] /Users/poupou/Library/Developer/CoreSimulator/Devices/1C6D73E1-2CB4-484D-A6E7-E0D57A39D3CB/data/Containers/Bundle/Application/4B428E5B-3C10-4F14-88C2-B85B5A508931/LinkerIssue.iOS.app/SharedClasses.dll was found

...

2015-04-08 13:52:25.159 LinkerIssue.iOS[74177:32710787] Unhandled managed exception:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list. (System.FormatException)
  at System.String.FormatHelper (System.Text.StringBuilder result, IFormatProvider provider, System.String format, System.Object[] args) [0x0010b] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/String.cs:2005 
  at System.String.Format (IFormatProvider provider, System.String format, System.Object[] args) [0x00000] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/String.cs:1951 
  at System.String.Format (System.String format, System.Object[] args) [0x00000] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/String.cs:1946 
  at LinkerIssue.BadArrayJsonTest.ToString () [0x0000c] in /Users/poupou/Downloads/LinkerIssue/LinkerIssue/BadArrayJsonTest.cs:17 
  at System.String.Concat (System.Object arg0, System.Object arg1) [0x00000] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/String.cs:2086 
  at LinkerIssue.App.TestConversion_EmptyArray () [0x0000e] in /Users/poupou/Downloads/LinkerIssue/LinkerIssue/LinkerIssue.cs:42 
  at LinkerIssue.App..ctor () [0x00008] in /Users/poupou/Downloads/LinkerIssue/LinkerIssue/LinkerIssue.cs:14 
  at LinkerIssue.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x00007] in /Users/poupou/Downloads/LinkerIssue/iOS/AppDelegate.cs:17 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/poupou/git/xamarin/maccore/src/UIKit/UIApplication.cs:63 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/poupou/git/xamarin/maccore/src/UIKit/UIApplication.cs:47 
  at LinkerIssue.iOS.Application.Main (System.String[] args) [0x0005c] in /Users/poupou/Downloads/LinkerIssue/iOS/Main.cs:20 


^ Note that the TypeLoadException is gone. There's a new one, that I assume to be normal (given the empty array input).

That's pretty much identical to what I saw last night. I'll now compare this to the 2nd sample.
Comment 21 George Cook 2015-04-08 14:10:11 UTC
thanks. I would be so happy if I got the array index exception, that is the expected behaviour. God.. I feel like when my wife was in labour.. This is a really important issue for me, I'm set to give a glowing review of xamarin studio next week to our whole company, and I've literally got a draft email postponing the whole thing which I'll have to send out tomorrow if I can't get the app size down :(

I know you guys are snowed, and I don't *yet* have any enterprise license; so I can't thank you enough for looking into this. I'll shut up and wait now :)
Comment 22 Sebastien Pouliot 2015-04-08 16:10:36 UTC
The 2nd sample https://bugzilla.xamarin.com/attachment.cgi?id=10668 is a bit different, i.e.:

* The main project is set to "Link SDK";
* It has additional arguments for mtouch: --linkskip=SharedClasses,LinkerIssue
* the LinkerIssue.iOS.exe already has a reference to SharedClasses.dll
* it has code that will ensure the C# compiler will include a reference to SharedClasses.dll

>	var dummy = typeof(SharedClasses.JsonCreationConverter<object>).ToString ();

If executed with XI 8.10 I get the TypeLoadException

2015-04-08 14:08:59.769 LinkerIssue.iOS[74509:32783343] completed {
  "items": []
}
completed {
  "items": []
}
2015-04-08 14:09:16.009 LinkerIssue.iOS[74509:32783343] Unhandled managed exception:
Could not load type SharedClasses.FixBadEmptyJsonArrayConverter`1[[System.String[], System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]], SharedClasses, Version=1.0.5576.25459, Culture=neutral, PublicKeyToken=null while decoding custom attribute (System.TypeLoadException)
  at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesInternal (System.Reflection.ICustomAttributeProvider,System.Type,bool)
  at System.MonoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType, Boolean inheritedOnly) [0x00019] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:103 
  at System.MonoCustomAttrs.GetCustomAttributes (ICustomAttributeProvider obj, System.Type attributeType, Boolean inherit) [0x00040] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/MonoCustomAttrs.cs:152 
  at System.Attribute.GetCustomAttributes (System.Reflection.MemberInfo element, System.Type type, Boolean inherit) [0x00016] in /Users/poupou/git/xamarin/mono/mcs/class/corlib/System/Attribute.cs:232 


The workaround it to change the additional mtouch arguments from

`--linkskip=SharedClasses,LinkerIssue` (which does nothing for Link SDK)

to

--linkskip=System.Runtime

which will preserve the PCL facade assembly on which type (inside the custom attribute) has it's reference. That will give you almost all the benefit from Link SDK (size wise) as System.Runtime.dll is almost empty (11kb of metadata).

Such a build gives me:

...
2015-04-08 16:09:15.565 LinkerIssue.iOS[91710:33142190] completed {
  "items": []
}
completed {
  "items": []
}
2015-04-08 16:09:17.511 LinkerIssue.iOS[91710:33142190] Unhandled managed exception:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list. (System.FormatException)
...
which is what you're expecting (and hopefully will be able to duplicate on your computers).


Something similar was fixed recently (which might be why I could not reproduce with our master branch) but that case can still happen (with the upcoming 8.10) so I need to double check why (as it should have been back ported). Thanks again for the details!
Comment 23 George Cook 2015-04-08 16:33:33 UTC
No, thank you looking into this.

I can confirm that the skip flag you provided fixes the issue in the example project here AND in my real project too.

app size is down to 56mb, which while a bit big (native app is 18mb), is presentable and easily justifiable with all the other benefits I'm going to layout!

I'll update the forum to make sure this answer is easy to find. This would've been impossible for me to resolve without all your help: Even on the forum, with considerable input from many experiences c# and xamarin developers, this was a dead end.

Thanks again!
Comment 24 Sebastien Pouliot 2015-04-10 17:36:03 UTC
Fixed in mono/master 5186dea237916e063b01ed75f1117b6709b92a53

Keeping bug open as maccore repo is not bumped to use that revision/branch at the moment.
Comment 25 Sebastien Pouliot 2015-04-23 11:10:12 UTC
QA: build c3733064e7f9d9f5da3f5ccffcf099210f9f2c07  (and more recent builds) from maccore/master can be used to very this