Bug 21303 - Random InvalidCastException (Runtime caching objects too long)
Summary: Random InvalidCastException (Runtime caching objects too long)
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on: 19718
Blocks:
  Show dependency tree
 
Reported: 2014-07-14 07:43 UTC by Nick Rymanov
Modified: 2015-04-23 08:24 UTC (History)
4 users (show)

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


Attachments
Test project (13.51 KB, application/zip)
2014-07-14 08:56 UTC, Nick Rymanov
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 ANSWERED

Description Nick Rymanov 2014-07-14 07:43:38 UTC
In our application we have a lot of InvalidCastException. It can happens in completely unpredictable places of code like

color = UIColor.FromRGBA(0f/255f, 239F/255f, 0f/255f, 1f);

Call stack:
[InvalidCastException: Unable to cast object of type 'Latista.Model.Item' to type 'MonoTouch.UIKit.UIColor']
    MonoTouch.ObjCRuntime.Runtime.GetNSObject[T](IntPtr ptr) in <filename unknown>:0
    MonoTouch.UIKit.UIColor.FromRGBA(Single red, Single green, Single blue, Single alpha) in <filename unknown>:0
    LApp.SomeViewController.UpdateProgressView() in <filename unknown>:0


Seems, it is related to Runtime.RegisterNSObject method, where Runtime creates a long WeakReference to NSObject (Runtime.object_map[ptr] = new WeakReference(obj, true)).

As result, GetNSObject<T>(IntPtr ptr) retrieves finalized, but not yet collected object from internal cache. 

=== Xamarin Studio ===

Version 5.1.2 (build 0)
Installation UUID: 28d70b5a-eff2-4981-b8d0-7163c4cfb39f
Runtime:
	Mono 3.4.0 ((no/954ed3c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000214

=== Xamarin.Android ===

Version: 4.12.6 (Business Edition)
Android SDK: /Users/nrymanov/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		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.2.5.5 (Business Edition)
Hash: 87026b6
Branch: 
Build date: 2014-07-02 19:39:08-0400

=== Xamarin.Mac ===

Version:

=== Build Information ===

Release ID: 501020000
Git revision: 4574b1e5cf09f2e95e486c259bd9267a68c77cb3
Build date: 2014-07-02 20:50:40-04
Xamarin addins: fdea6fe3e36cda39291b39c81133e409c6d6f092

=== Operating System ===

Mac OS X 10.9.4
Darwin NickRymanov-Mac-mini-5.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 1 Udham Singh 2014-07-14 08:40:59 UTC
We have tried to reproduce this issue but not able to reproduce this. To reproduce this issue we have implemented the code like "color = UIColor.FromRGBA(0f/255f, 239F/255f, 0f/255f, 1f);" in an iOS application and for us it is working fine.

Screencast : http://screencast.com/t/YjSMnseKvu

Could you please provide a sample project or test case or steps, so that we can reproduce this issue at our end.

Environment Info : 

=== Xamarin Studio ===

Version 5.1.2 (build 0)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
	Mono 3.4.0 ((no/954ed3c)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000214

=== Xamarin.Android ===

Version: 4.12.5 (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 (5084)
Build 5B130a

=== Xamarin.iOS ===

Version: 7.2.5.5 (Enterprise Edition)
Hash: 87026b6
Branch: 
Build date: 2014-07-02 19:39:08-0400

=== Xamarin.Mac ===

Version:

=== Build Information ===

Release ID: 501020000
Git revision: 4574b1e5cf09f2e95e486c259bd9267a68c77cb3
Build date: 2014-07-02 20:50:40-04
Xamarin addins: fdea6fe3e36cda39291b39c81133e409c6d6f092

=== 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 2 Nick Rymanov 2014-07-14 08:56:01 UTC
Created attachment 7354 [details]
Test project

I can't create a project to reproduce described problem. As I said before, the problem occurs randomly.

The attached project demonstrate the problem with long-lived WeakReference. Since Runtime uses the same logic in GetNSObject/TryGetNSObject methods i think it could be a source of original problem.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-07-15 13:02:17 UTC
This is probably a dup of #19718.
Comment 4 Nick Rymanov 2014-07-17 06:33:58 UTC
This is a real blocker for our application. We have tens crashes everyday and our clients and top managers are really disappointed with a new version of our product (rewritten from Objective-C).
Comment 6 lpinho 2014-07-18 04:16:54 UTC
Hi, same problem here:

Unhandled managed exception: Unable to cast object of type 'ZXing.Mobile.ZXingScannerViewController' to type 'MonoTouch.CoreMedia.CMSampleBuffer'. (System.InvalidCastException)

As you can imagine, there is no place in the code that something tries to cast a view controller to a CMSampleBuffer, I and the developer of ZXing have looked for it in all the code and nothing was found.

This also happens randomly, 99% of the times everything works fine.
Comment 10 Rolf Bjarne Kvinge [MSFT] 2015-04-16 10:21:11 UTC
Nick, can you still reproduce this bug with the current stable release?
Comment 12 Rolf Bjarne Kvinge [MSFT] 2015-04-23 08:24:58 UTC
OK, I'm closing this then.

Please reopen and attach a project we can use to reproduce any issues if you want us to have a second look.