Bug 4864 - System.InvalidCastException:: MonoTouch.UIKit.UIColor.FromRGBA
Summary: System.InvalidCastException:: MonoTouch.UIKit.UIColor.FromRGBA
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
: 5139 ()
Depends on:
Blocks:
 
Reported: 2012-05-03 11:13 UTC by René Ruppert
Modified: 2012-08-04 07:36 UTC (History)
4 users (show)

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


Attachments
Sample project (8.32 KB, application/zip)
2012-05-19 02:56 UTC, René Ruppert
Details


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 FIXED

Description René Ruppert 2012-05-03 11:13:23 UTC
CRASH @ 03.05.2012 15:33:27
Using UIColor.FromRGB(110, 112, 119) I get the exception below in MT 5.3.3

MONOTOUCH VERSION: 5.3.3
BRAINLOOP MOBILE VERSION: 2.1.0.120503
TERMINATING:True
EXCEPTION:
System.TypeInitializationException: An exception was thrown by the type initializer for BrainloopMobile.UIHelpers ---> System.InvalidCastException: Cannot cast from source type to destination type.
 at MonoTouch.UIKit.UIColor.FromRGBA (Single red, Single green, Single blue, Single alpha) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.g.cs:127
 at MonoTouch.UIKit.UIColor.FromRGB (Byte red, Byte green, Byte blue) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.cs:25
 at MonoTouch.UIKit.UIColor.FromRGB (Int32 red, Int32 green, Int32 blue) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIColor.cs:30
 at BrainloopMobile.UIHelpers..cctor () [0x00000] in <filename unknown>:0
 --- End of inner exception stack trace ---
 at BrainloopMobile.AppDelegateBase.FinishedLaunching (MonoTouch.UIKit.UIApplication application, MonoTouch.Foundation.NSDictionary launchOptions) [0x000bc] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/AppDelegateBase.cs:662
 at BrainloopMobile.AppDelegateIPad.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00000] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/AppDelegateIPad.cs:24
 at MonoTouch.UIKit.UIApplication.SendEvent (MonoTouch.UIKit.UIEvent uievent) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.g.cs:175
 at BrainloopMobile.UIApplicationMain.SendEvent (MonoTouch.UIKit.UIEvent oEvent) [0x00000] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/UIApplicationMain.cs:23
 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 BrainloopMobile.Application.Main (System.String[] args) [0x00000] in /Users/rene/Documents/Develop/Projects/BrainloopMobile/210/BrainloopMobile/BrainloopMobile/Main.cs:15
Comment 1 Sebastien Pouliot 2012-05-03 15:40:32 UTC
Trying this:
			var c = UIColor.FromRGB (110, 112, 119);
works fine for me. Could it be other values ?

Is this on the simulator or device ?

I'll try to see which values could lead to this.
Comment 2 Sebastien Pouliot 2012-05-03 16:14:39 UTC
That exception should occurs only if the generated code returns something that is *not* an UIColor.

Can you attach the full build log (with extra -v -v -v) that provoke this. Some other options might play a role in this. Thanks
Comment 3 René Ruppert 2012-05-04 02:58:11 UTC
I currently cannot upgrade to MT 5.3.3.

But this happened on the device.
It happened in this code:


	public static void InitGlobalStyleSettings ()
		{
			UIBarButtonItem.Appearance.TintColor = UIHelpers.TINT_COLOR_BAR_BUTTON_ITEM;
			UISegmentedControl.Appearance.TintColor = UIHelpers.TINT_COLOR_SEGMENTED_CONTROL;
			UISwitch.Appearance.OnTintColor = UIHelpers.TINT_COLOR_SWITCH;
			UILabel.Appearance.BackgroundColor = UIColor.Clear;
		}

InitGlobalStyleSettings() gets called from FinishedLaunching.
The color values are statics:

public static UIColor TINT_COLOR_BAR_BUTTON_ITEM = UIColor.FromRGB(102, 112, 119);
public static UIColor TINT_COLOR_SEGMENTED_CONTROL = UIColor.FromRGB(102, 112, 119);
public static UIColor TINT_COLOR_SWITCH = UIColor.FromRGB(102, 112, 119);
Comment 4 Sebastien Pouliot 2012-05-04 08:25:41 UTC
do you recall (or still have the same) build options ? (e.g. extra mtouch arguments, GC, LLVM, linker...) if so then it would still b euseful to have a build log
Comment 5 René Ruppert 2012-05-07 05:00:21 UTC
No extra arguments (except the one required to overcome the trampolines problems that you asked to report bugs about in 5.3.3 - i don't remember what they were called).
LLVM on, SGen on, ARM v7 - all other settings default.
I can retry once we have that beast released.
Comment 6 Sebastien Pouliot 2012-05-07 09:21:50 UTC
The same code, under the same config, does not fail for me. There's likely something else in your solution that cause this, indirectly, to happen. If you can, please attach a test case (or a subset of the app) that show this and we'll investigate it from there. Thanks!
Comment 7 René Ruppert 2012-05-19 02:56:44 UTC
Created attachment 1922 [details]
Sample project
Comment 8 Sebastien Pouliot 2012-05-19 10:11:12 UTC
This occurs because of --noregistrar. The application cannot somehow (the bug) register monotouch.dll properly. MD application output shows:

2012-05-19 10:05:32.118 GCTest[11345:707] Could not load 'monotouch' for registration. This could be due to an outdated assembly kept by the simulator, location: /private/var/mobile/Applications/39F6D08B-B6F0-4550-9632-CFBD66939765/GCTest.app/monotouch.dll

The bug still exists in 5.3.4 (noregistrar) but the option is not needed anymore (at least for the sample).
Comment 9 Sebastien Pouliot 2012-05-19 10:54:20 UTC
Now fixed to provide a better error message:

2012-05-19 10:32:35.661 GCTest[11376:707] Could not load 'monotouch' for registration: System.Exception: Wrapper type 'MonoTouch.UIKit.UIAppearance' is missing its native ObjectiveC class 'UIAppearance'.
  at MonoTouch.ObjCRuntime.Class.Register (System.Type type, System.String name, Boolean is_wrapper) [0x006ab] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Class.cs:356 
  at MonoTouch.ObjCRuntime.Class.Register (System.Type type) [0x0004f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Class.cs:191 
  at MonoTouch.ObjCRuntime.Runtime.RegisterAssembly (System.Reflection.Assembly a) [0x0009f] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:96 

^ UIAppearance is a bit of a special case, but its a protocol and was not marked as [Model]. That does not play well with the checks done when --noregistrar was used.

Testing...
Comment 10 Sebastien Pouliot 2012-05-19 12:04:56 UTC
Fixed. Thanks for the test case!

master: 6cee930b5e8987376628dbdabf98d7d02201fcb2
5.2-series: a92fd8d0538bb15fccd6487adc984cd21c0b4ba0

QA: you'll need to copy-paste some existing tests (e.g. this one or any existing samples) and build them using --noregistrar and ensure they work on device.
Comment 11 Sebastien Pouliot 2012-05-19 12:14:25 UTC
*** Bug 5139 has been marked as a duplicate of this bug. ***
Comment 12 René Ruppert 2012-05-20 05:39:38 UTC
The "--noregistrar" indeed isn't needed for that sample but it is required to demonstrate the problem. My real app needs the additional option however. But you say the option is gone in future versions of MT?
Comment 13 Sebastien Pouliot 2012-05-20 23:55:30 UTC
> The "--noregistrar" indeed isn't needed for that sample

Ok. The fix was tested with both --noregistrar and without it.

> My real app needs the additional option however.

Hopefully that will be fixed too (was there a bug for it?) in the next release.

> you say the option is gone in future versions of MT?

No, sorry if it sounded confusing about "not being needed". To be clear the --noregistrar option is not removed (it's still in 5.3.4).
Comment 14 Mikkel 2012-08-03 16:24:32 UTC
Is this fixed in 5.99? I'm seeing something similar with UIColor.ColorWithAlpha.

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

And for a feature in my own project it's UIColor.FromCGColor (somewhat based on the above sample), as well as some issues with UIImage.FromFile being called from an Action delegate, which is called from a CALayer.DrawInContext. Unfortunately, I haven't had time create a test case on those (yet).
Comment 15 Mikkel 2012-08-04 07:36:58 UTC
I should add, that 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.

Specific versions:

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

Let me know if I should open a separate bug report.

Thanks