Bug 31302 - App crashes with message "Cannot create an instance of UIKit.UIBarItem because it is an abstract class" on device only.
Summary: App crashes with message "Cannot create an instance of UIKit.UIBarItem becaus...
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2015-06-22 16:54 UTC by Jon Goldberger [MSFT]
Modified: 2016-02-18 15:49 UTC (History)
3 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-06-22 16:54:48 UTC
Created attachment 11708 [details]
Test Project

## Description

App crashes with message "Cannot create an instance of UIKit.UIBarItem because it is an abstract class", but only with "Link SDK Assemblies Only" option and only when deploying to a device. 

Issue does not occur with Linker setting "Don't Link" or when deploying to a simulator with either linker option.

There is nothing in the code trying to instantiate this class directly, however there is a class created by PaintCode in the resources folder that is referenced in the Main.storyboard file. Removing the references to MedOneStyleKit in the Main.storyboard file also resolves this issue (but obviously at the cost of the user's styles not being used.)

## Steps to reproduce.

1. Open the attached test project and deploy to an iOS device.

Expected result: App will deploy and launch the first UI screen.

Actual result: App crashes on Launch screen with following exception:

## Stack trace:

>System.MemberAccessException: Cannot create an instance of UIKit.UIBarItem because it is an abstract class
>  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00095] in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:526
>  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:554
>  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/1503/e6ebd18b/source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62
>  at ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in <filename unknown>:0
>  at ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass, MissingCtorResolution missingCtorResolution) [0x00013] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/ObjCRuntime/Runtime.cs:666
>  at ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, MissingCtorResolution missingCtorResolution, Boolean evenInFinalizerQueue) [0x00022] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/ObjCRuntime/Runtime.cs:764
>  at ObjCRuntime.Runtime.TryGetOrConstructNSObjectWrapped (IntPtr ptr) [0x00000] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/ObjCRuntime/Runtime.cs:355
>  at ObjCRuntime.Runtime.try_get_or_construct_nsobject (IntPtr obj) [0x00000] in <filename unknown>:0
>  at at (wrapper native-to-managed) ObjCRuntime.Runtime:try_get_or_construct_nsobject (intptr)
>  at 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/builder/data/lanes/1503/e6ebd18b/source/maccore/src/UIKit/UIApplication.cs:63
>  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/1503/e6ebd18b/source/maccore/src/UIKit/UIApplication.cs:46
>  at MedOne.iOSApp.Application.Main (System.String[] args) [0x00008] in /Users/apple/Downloads/KeepForNow/176115/UIBarButtonItem Crash/MedOne.iOSApp/Main.cs:17

## Environment info:

=== Xamarin Studio ===

Version 5.9.3 (build 1)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
	Mono 4.0.1 ((detached/ed1d3ec)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400010044

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 6.3.2 (7718)
Build 6D2105

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: e6ebd18
Branch: master
Build date: 2015-05-21 21:55:09-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 509030001
Git revision: 5a524e1726ed103fdd4fe37e0356f2b35466ce9d
Build date: 2015-06-02 16:35:08-04
Xamarin addins: 51957cfbd06be911b212671ad05c2c6221ac90f9

=== Operating System ===

Mac OS X 10.10.3
Darwin Jons-iMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 2 Sebastien Pouliot 2015-06-25 09:01:26 UTC
That sounds like the application itself does not use the UIBarButtonItem or UITabBarItem (both subclasses of UIBarItem) but it's indirectly referenced by the storyboard, i.e. it tries to create them at runtime (similar to reflection).

In general that does not happen because UI elements have corresponding code back in the application (that the linker will see).

I'll confirm that and, if it's the case, provide a workaround.
Comment 3 Sebastien Pouliot 2015-06-25 10:33:03 UTC
So the customization code from PaintCode is not strongly-typed, e.g.

        public NSObject[] ETabBarTargets
            get { return eTabBarTargets; }
                eTabBarTargets = value;
                foreach (NSObject target in value)
                    target.PerformSelector(new ObjCRuntime.Selector("setImage:"), ImageOfETabBar, 0);

but it does expose the `UITabBarItem` to managed code. Ideally that would be fixed in the MedOneStyleKit.cs code, minimally like this:

        public NSObject[] TruckTabBarTargets
            get { return truckTabBarTargets; }
                truckTabBarTargets = value;
                foreach (UITabBarItem target in value)
			target.Image = ImageOfTruckTabBar;

But that requires changes to PaintCode itself or changes to generated code (harder to maintain).

Another, easier solution is to add this line anywhere in the project (e.g. in AppDelegate.cs)

	[Preserve (typeof (UITabBarItem))]

That will ensure there's a reference to `UITabBarItem` and that the linker will keep it around (so the existing code can work unmodified).
Comment 4 jannic.benitz 2016-02-18 15:49:20 UTC
Adding [Preserve (typeof (UITabBarItem))] does not fix the Problem!