Bug 1825 - InvalidCastException when using only static methods to create Objective-C objects
Summary: InvalidCastException when using only static methods to create Objective-C obj...
Status: RESOLVED FIXED
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: GIT
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-02 10:05 UTC by Dan Fry
Modified: 2013-02-01 09:34 UTC (History)
2 users (show)

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


Attachments
MD project to reproduce bug (2.91 KB, application/zip)
2011-11-02 10:06 UTC, Dan Fry
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 Dan Fry 2011-11-02 10:05:20 UTC
If a MonoMac type is created, but its constructor has never been called (for example, by calling a static method such as NSNumber.FromInt32()), an InvalidCastException is raised. This appears to be because an entry is only added to Class.type_map when a type's constructor is first called.

In addition, in the event that none of the constructors for the ancestors of the class have been called, Class.Lookup() enters an infinite loop. (This appears to have the same cause as bug 408).

This bug appears to be present in the latest revision of MonoMac in git, at the time of writing.

The attached project reproduces the bug but must be used with a build of MonoMac from git, as the version supplied with MonoDevelop 2.8.1 appears to cause the debugger to hang.
Comment 1 Dan Fry 2011-11-02 10:06:06 UTC
Created attachment 808 [details]
MD project to reproduce bug
Comment 2 Sebastien Pouliot 2013-02-01 09:34:32 UTC
You need to call:

MonoMac.AppKit.NSApplication.Init ();

in your console application, otherwise not everything will be initialized properly.

Also recent version (tried with Xamarin.Mac) won't enter an infinite loop, you'll get:

System.ArgumentException: Could not find a valid superclass for type __NSCFNumber. Did you forget to register the bindings at MonoMac.ObjCRuntime.Class.Register() or call NSApplication.Init()?
  at MonoMac.ObjCRuntime.Class.Lookup (IntPtr klass) [0x000ab] in /Users/poupou/git/mono/xamcore/monomac/src/ObjCRuntime/Class.cs:118 

which (hints you about the missing NSApplication.Init call).