Bug 6426 - InvalidCastException with UIColor and UIImage
Summary: InvalidCastException with UIColor and UIImage
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.3.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
: 6556 ()
Depends on:
Blocks:
 
Reported: 2012-08-06 14:44 UTC by Mikkel
Modified: 2014-02-11 10:59 UTC (History)
9 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:
VERIFIED FIXED

Description Mikkel 2012-08-06 14:44:51 UTC
I'm having some issues where I'm getting InvalidCastException a few places. One of them is with "System.InvalidCastException: Unable to cast object of type 'CustomPropertyAnimation.CircleLayer' to type 'MonoTouch.UIKit.UIColor'."

Test case:
https://github.com/xamarin/monotouch-samples/tree/monotouch-5.4/CustomPropertyAnimation

It can happen within a few seconds in the linked test case
(both simulator and device - a bit longer on the device for some reason). It
happens immediately with my own project.

Stack trace from the simulator, CustomPropertyAnimation:

System.InvalidCastException: Unable to cast object of type 'CustomPropertyAnimation.CircleLayer' to type 'MonoTouch.UIKit.UIColor'.
  at MonoTouch.UIKit.UIColor.ColorWithAlpha (Single alpha) [0x0000b] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:222
  at CustomPropertyAnimation.CircleLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x00036] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:124
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at CustomPropertyAnimation.AppDelegate.Main (System.String[] args) [0x00000] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:70

Stack trace from the device, CustomPropertyAnimation:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MonoTouch.UIKit.UIColor.ColorWithAlpha (Single alpha) [0x00003] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:222
  at CustomPropertyAnimation.CircleLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x00036] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:124
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at CustomPropertyAnimation.AppDelegate.Main (System.String[] args) [0x00000] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:70

Device log, CustomPropertyAnimation:

Aug  6 19:55:17 ML-iPhone4 CustomPropertyAnimation[4894] <Warning>:
Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
at MonoTouch.UIKit.UIColor.ColorWithAlpha (Single alpha) [0x00003] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:222
at CustomPropertyAnimation.CircleLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x00036] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:124
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at CustomPropertyAnimation.AppDelegate.Main (System.String[] args) [0x00000] in SNIPPED/monotouch-samples/CustomPropertyAnimation/AppDelegate.cs:70
Aug  6 19:55:17 ML-iPhone4 CustomPropertyAnimation[4894] <Critical>: Terminating runtime due to unhandled exception
Aug  6 19:55:18 ML-iPhone4 ReportCrash[4898] <Notice>: Formulating crash report for process CustomPropertyAnimation[4894]
Aug  6 19:55:18 ML-iPhone4 com.apple.launchd[1] (UIKitApplication:com.xamarin.custompropertyanimation[0x1ee6][4894]) <Warning>: (UIKitApplication:com.xamarin.custompropertyanimation[0x1ee6]) Job appears to have crashed: Abort trap: 6
Aug  6 19:55:18 ML-iPhone4 backboardd[52] <Warning>: Application 'UIKitApplication:com.xamarin.custompropertyanimation[0x1ee6]' exited abnormally with signal 6: Abort trap: 6
Aug  6 19:55:18 ML-iPhone4 ReportCrash[4898] <Notice>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/CustomPropertyAnimation_2012-08-06-195517_ML-iPhone4.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0

Stack trace from the simulator, my project: // Seems very similar.

System.InvalidCastException: Unable to cast object of type 'SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer' to type 'MonoTouch.UIKit.UIColor'.
  at MonoTouch.UIKit.UIColor.FromCGColor (MonoTouch.CoreGraphics.CGColor color) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:161
  at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x00061] in SNIPPED/UI/Profile/ProfileNavigationController.cs:672
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at SNIPPED.Application.Main (System.String[] args) [0x00000] in SNIPPED/Main.cs:21

Stack trace from device, my project:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MonoTouch.UIKit.UIImage.FromFile (System.String filename) [0x0001e] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIImage.g.cs:203
  at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController.<CreateBlueGauge>m__9F (Single value) [0x00057] in SNIPPED/UI/Profile/ProfileNavigationController.cs:544
  at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x000bb] in SNIPPED/UI/Profile/ProfileNavigationController.cs:689
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at SNIPPED.Application.Main (System.String[] args) [0x00000] in SNIPPED/Main.cs:21

Device log, my project:

Aug  6 19:59:33 ML-iPhone4 SNIPPED[4955] <Warning>:
Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
at MonoTouch.UIKit.UIColor.FromCGColor (MonoTouch.CoreGraphics.CGColor color) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:161
at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x00061] in SNIPPED/UI/Profile/ProfileNavigationController.cs:672
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at SNIPPED.Application.Main (System.String[] args) [0x00000] in SNIPPED/Main.cs:21
Aug  6 19:59:33 ML-iPhone4 SNIPPED[4955] <Critical>: Terminating runtime due to unhandled exception
Aug  6 19:59:34 ML-iPhone4 ReportCrash[4957] <Notice>: Formulating crash report for process SNIPPED[4955]
Aug  6 19:59:34 ML-iPhone4 com.apple.launchd[1] (UIKitApplication:com.SNIPPED.mobile[0x90b2][4955]) <Warning>: (UIKitApplication:com.SNIPPED.mobile[0x90b2]) Job appears to have crashed: Abort trap: 6
Aug  6 19:59:34 ML-iPhone4 backboardd[52] <Warning>: Application 'UIKitApplication:com.SNIPPED.mobile[0x90b2]' exited abnormally with signal 6: Abort trap: 6
Aug  6 19:59:34 ML-iPhone4 ReportCrash[4957] <Notice>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/SNIPPED_2012-08-06-195933_ML-iPhone4.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0

Let me know if you need the crashlogs -- for some reason I can't get a symbolicated version right now :-/

I also see InvalidCastException with UIImage (if I do a small workaround with the issue above):

System.InvalidCastException: Unable to cast object of type 'SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer' to type 'MonoTouch.UIKit.UIImage'.
  at MonoTouch.UIKit.UIImage.FromFile (System.String filename) [0x0001e] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIImage.g.cs:203
  at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController.<CreateBlueGauge>m__9F (Single value) [0x00057] in SNIPPED/UI/Profile/ProfileNavigationController.cs:544
  at SNIPPED.UI.Profile.ProfileNavigationController+ProfileDialogViewController+GaugeIndicatorLayer.DrawInContext (MonoTouch.CoreGraphics.CGContext context) [0x0009b] in SNIPPED/UI/Profile/ProfileNavigationController.cs:690
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at SNIPPED.Application.Main (System.String[] args) [0x00000] in SNIPPED/Main.cs:21

Let me know if you need a separate test case for that.

My setup:

MT: 5.99.1 (using Xcode 4.5 DP3)
MD: 3.0.4.2
OS: Mountain Lion

Sorry about all the secrecy.

Thanks,
Mikkel
Comment 1 Mikkel 2012-08-10 07:29:12 UTC
Upgraded to MD 3.0.4.3 and MT 5.99.2 (using Xcode 4.5 DP4) and the issue is still there.

Mikkel
Comment 2 Sebastien Pouliot 2012-08-13 10:50:29 UTC
The same exception occurs using MonoTouch 5.3.x on Snow Leopard.
Comment 3 Sebastien Pouliot 2012-08-13 11:29:13 UTC
Got the feeling we're not surfacing the right (expected) type.
This also occurs when using `sgen` and `sgen+newrefcount`
Comment 4 Miguel de Icaza [MSFT] 2012-08-18 00:05:52 UTC
This is fixed on our 5.4 branch, and I added it to the 5.3.6 branch, not sure if the final 5.3.6.0 wil have it.
Comment 5 Mikkel 2012-08-18 06:28:39 UTC
Great! Will the next 5.99.X alpha have it? If you'd like I can also test it with my own project before it hits the release channels?
Comment 6 Mikkel 2012-08-29 10:05:15 UTC
I guess it's not fixed in 5.3.6 as I still have the issue (and so does the repo I linked to). When can I expect it in one of the release channels? I'd really like to see it in the 5.99 series, as I have some iOS 6 features to test.
Comment 7 Sebastien Pouliot 2012-08-29 10:21:28 UTC
AFAICT the patch is part of 5.3.6 (more recent patches from that branches were required for gatekeeper signing).

PJ, can you (or someone else ;-) confirm if this works (or not) using the 5.3.6 package (as released).
Comment 9 PJ 2012-08-29 16:34:56 UTC
*** Bug 6556 has been marked as a duplicate of this bug. ***
Comment 10 Sebastien Pouliot 2012-08-30 10:59:32 UTC
Can't duplicate on simulator (anyone can ?) 
but I could get it on device (re-opening)
Comment 11 Mikkel 2012-08-30 11:01:40 UTC
Sorry, should have been more clear. It's also only happening on device for me.
Comment 12 Sebastien Pouliot 2012-08-30 17:01:33 UTC
Thanks for confirming it's device-only :-)

There's an issue with the registrar, we're not hitting the fix that was made earlier.
You should be able to work around it using --noregistrar until this is fixed.
Comment 13 Sebastien Pouliot 2012-08-30 17:53:28 UTC
The new, specialize (specially crafted) ctor trampoline, also needed the same fix as the "general" case (used in the simulator and when --noregistrar is used).

Fixed in
monotouch-5.4-series: 80817d7380c24f25470e16017ddb2d0129176d44
master: e4077aa41c3be8a307ed9705ce5fa392fa93f328
ios6: 50b28817f1f2b462ba980fa302fcf8fccd978a07
Comment 14 Mikkel 2012-08-31 05:09:13 UTC
Thanks, I'll try that right away!
Comment 15 Mohit Kheterpal 2013-06-17 13:00:58 UTC
Today we have checked this issue on following builds :

XS 4.0.8 
MT 6.3.6.76
Mono 3.0.11
Xcode 4.6.2
MTVS 1.1.200

We are able to run / debug application on device and simulator successfully. Without any Exception.

Hence closing this issue. Changing its status to verified fixed.
Comment 16 Mikkel 2013-06-17 14:34:46 UTC
Thanks for the update. You mention Mono 3.0.11 — has it been released?
Comment 17 Peter Collins 2013-06-17 14:51:20 UTC
Mono 3.0.10 is available on our Beta and Alpha channels currently. Since the fix for this landed ~10 months ago it's a safe bet that the products in all three Stable, Beta, and Alpha channels contain this fix.
Comment 18 Mikkel 2013-06-17 15:02:47 UTC
Yes, I've been using that version for a while now. He mentioned .11 so I wondering if it had been released. I now see that he's not from Xamarin, so it could be a typo (or from repo?).
Comment 19 pperera 2014-02-10 21:11:44 UTC
I'm still seeing this issue in very similar cases, to the original in both UIColor and UIImage

Xamarin Studio
Version 4.2.2 (build 2)

Xamarin.iOS
Version: 7.0.5.2 (Enterprise Edition)
Hash: 9c42159
Branch: 
Build date: 2013-02-12 20:04:08-0500
Comment 20 Sebastien Pouliot 2014-02-10 21:38:28 UTC
There can be many reasons for an InvalidCastException (ICE) and that one should be fixed. 

@PJ: just to be 100% certain it's not a regression could you have someone confirm the original test case is working properly ?

@pperera: Do you have a test case that can reproduce your ICE ? if so please open a new bug report and attach the test case to it.
Comment 21 Mohit Kheterpal 2014-02-11 10:59:06 UTC
Today we have checked this issue with following builds :

XS 4.2.2 (build 2)
X.iOS 7.0.5.2

CustomPropertyAnimation sample is still working fine on device.