Bug 22040 - InvalidCastException for a UIView subclass derived from a binding project
Summary: InvalidCastException for a UIView subclass derived from a binding project
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.2.6
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Alex Soto [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2014-08-12 12:57 UTC by John Miller [MSFT]
Modified: 2014-08-22 17:08 UTC (History)
5 users (show)

Tags:
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:
Status:
RESOLVED INVALID

Description John Miller [MSFT] 2014-08-12 12:57:56 UTC
**Overview:**

   The provided sample throws an exception when running on a simulator, but runs fine on the device.

**Steps to Reproduce:**

   1. Run the provided sample on the iOS Simulator

**Actual Results:**

   Unable to cast object of type 'MonoTouch.UIKit.UIView' (Objective-C type: 'UIView') to type 'AddLive.ALVideoView'.
Additional information:
	Selector: setViewVideo:
	Method: InvalidCastExceptionSample.InvalidCastExceptionSampleViewController:set_viewVideo (AddLive.ALVideoView)

**Expected Results:**

   No exception.

**Build Date & Platform:**

   XI 7.2.6

**Additional Information:**

   Works when running on an iOS device
Comment 1 Alex Soto [MSFT] 2014-08-12 13:11:14 UTC
Hello John

Would love yo help you here but we need a sample to work on ;)
Comment 3 John Miller [MSFT] 2014-08-12 15:35:31 UTC
Supplied sample.
Comment 4 Rajneesh Kumar 2014-08-13 09:56:46 UTC
I have checked this issue and able to reproduce this issue. To reproduce this I have followed the steps given in bug description. I observed that  when I run the attached sample application on iPhone simulator  getting the exception:

Unable to cast object of type 'MonoTouch.UIKit.UIView' (Objective-C type: 'UIView') to type 'AddLive.ALVideoView'.
Additional information:
	Selector: setViewVideo:
	Method: InvalidCastExceptionSample.InvalidCastExceptionSampleViewController:set_viewVideo (AddLive.ALVideoView)

I also observed that when I run the attached sample application on iPhone device it launch successfully.

Screencast: http://www.screencast.com/t/WNxilvwIx


Environment Info:

=== Xamarin Studio ===

Version 5.2 (build 386)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
	Mono 3.8.0 ((no/1a2c721)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000004

=== Xamarin.Android ===

Version: 4.14.0 (Enterprise Edition)
Android SDK: /Users/MM/Desktop/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)
		3.2   (API level 13)
		4.0   (API level 14)
		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)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.iOS ===

Version: 7.4.0.97 (Enterprise Edition)
Hash: 1400f0f
Branch: 
Build date: 2014-08-12 20:42:33-0400

=== Xamarin.Mac ===

Version: 1.10.0.4 (Enterprise Edition)

=== Build Information ===

Release ID: 502000386
Git revision: e6a54dee5376e6e7a2d9982695b060fddc09e65d
Build date: 2014-08-04 14:03:28-04
Xamarin addins: 2b5a5c26ac2ee74c6e91a8d24ef44d0ca9cb74d0

=== Operating System ===

Mac OS X 10.9.2
Darwin MacMini.local 13.1.0 Darwin Kernel Version 13.1.0
    Thu Jan 16 19:40:37 PST 2014
    root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
Comment 5 Alex Soto [MSFT] 2014-08-13 16:11:31 UTC
I can repro, still digging in.
Comment 6 Alex Soto [MSFT] 2014-08-15 11:25:32 UTC
Hello @John

Inside the dll there are two .a files libAddLive.a and libCustomServiceListener.a it seems that **libCustomServiceListener.a** is missing the simulator bits (i386) any chance we could get a fat library that has the i386 arch?
Comment 9 Alex Soto [MSFT] 2014-08-22 17:08:45 UTC
Hello

TL;DR: The libAddLive.a is not built correctly even an objective-c project would fail.

The issue is libAddLive.a, a library contained inside Binding.AddLive.iOS.dll. While it is a "fat" [1] library it is not built correctly, it does not contains the necessary objects to run on simulator (32/64 bits) nor on a 64 bit device on 64 bits mode (arm64).

You can verify this issue by extracting single architectures from the fat [1] library using lipo command like this:

    xcrun -sdk iphoneos lipo -thin armv7 libAddLive.a -output libAddLive-armv7.a

You need to repeat this step for each of the architectures contained on the library [2] in order to extract them all.

Once you extracted them all one of the first hints the library is not built correctly is the different sizes between the native libraries (most of the time they are almost the same size). 

>  3.1M Jun 12 libAddLive-arm64.a    <- Wrong
> 171M Jun 12 libAddLive-armv7.a    <- Good
> 171M Jun 12 libAddLive-armv7s.a   <- Good
>  3.0M Jun 12 libAddLive-i386.a        <- Wrong
>  3.6M Jun 12 libAddLive-x86_64.a    <- Wrong
> 351M Jun 12 libAddLive.a  <- Fat lib containing all of the above

armv7 and armv7s seems to contain all the required objects 171 MB (this is why it works on device)

arm64 (64 bit devices), i386 (simulator 32 bits) and x86_64 (simulator 64 bits) are only 3~ MB so not all the required objects are present within the library and thats why when we try to load the library on simulator you get the exception because we cannot instantiate the object and cast it to the correct type.

Now in order to verify that the objects are not present on the i386 arch you can run the following command

    ar -t libAddLive-i386.a | grep ALVideoView

this will print nothing, but if you run the command on the armv7 version of it

    ar -t libAddLive-armv7.a | grep ALVideoView

this will print

> ALVideoView.o
> ALVideoViewDrawCtx.o

The solution is to tell the customer to provide a fat library of libAddLive.a containing all the required symbols.

Hope this helps

Alex


[1]: A library that contains more than one architecture (armv7, armv7s, i386, x86_64, arm64, etc)
[2]: You can use "xcrun -sdk iphoneos lipo -info libAddLive.a" in order to print contained architectures.