Bug 8313 - InvalidCastException at runtime during release but not debug
Summary: InvalidCastException at runtime during release but not debug
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-11-09 09:04 UTC by Jeremy Kolb
Modified: 2012-11-09 16:25 UTC (History)
2 users (show)

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:

Description Jeremy Kolb 2012-11-09 09:04:22 UTC
I have a fragment that implements LoaderManager.ILoaderCallbacks:

        #region ILoaderCallbacks Members

        public Loader OnCreateLoader(int id, Bundle args)
            string[] projection = new string[]

            return new CursorLoader(Activity, Contract.Personnel.ContentUri, projection, null, null, null);

        public void OnLoadFinished(Loader loader, Java.Lang.Object data)
                case PersonnelListLoaderId:

        public void OnLoaderReset(Loader loader)


During debug this works fine but when running a release build the cast in OnLoadFinished fails.  Debugging shows that data is a CursorWrapperInner in both builds.
Comment 1 Jeremy Kolb 2012-11-09 09:26:05 UTC
I made a mistake... printing out the type of 'data' returns CursorWrapper in debug and object in Release.

This is on version 4.2.7 with Android 4.0.3 and the application targets 4.0.
Comment 2 Jeremy Kolb 2012-11-09 11:41:56 UTC
data.JavaCast<ICursor>() works around the issue but I have to do it in a lot of places.  Disabling linking gets around the issue.
Comment 3 Jonathan Pryor 2012-11-09 14:58:00 UTC
Could you provide a test case + instructions on how to execute it? This smells like a linker bug (but maybe not; see below), and a way to easily test it would be wonderful. :-)

> printing out the type of 'data' returns ... object in Release.

That's the problem (and why data.JavaCast<ICursor>() works as a workaround). The question is, why? My guess is that CursorWrapper is not statically referenced anywhere in your code, and thus the linker is (correctly!) removing the CursorWrapper type. If this is the case, then the only type that an android.database.CursorWrapper instance can be wrapped in is Java.Lang.Object, which appears to be what's happening.

Assuming this is the case, the fix is to get the linker to preserve the CursorWrapper type:

    // http://docs.xamarin.com/Android/Guides/Advanced_Topics/Linking#falseflag
    #pragma warning disable 0219, 0649
        static bool falseflag = false;
        static MyActivity ()
            if (falseflag) {
                var c = new CursorWrapper(null);
    #pragma warning restore 0219, 0649    if (falseflag) {

This should fix your Release crash.
Comment 4 Jeremy Kolb 2012-11-09 16:23:23 UTC
My bad it's not a CursorWrapper it's "android.content.ContentResolver$CursorWrapperInner".  Your trick seems to work.  It's a bummer that I have to do this though.
Comment 5 Jonathan Pryor 2012-11-09 16:25:12 UTC
The linker isn't all-knowing (and attempts to do so would likely be brittle). :-(

Fortunately the workarounds aren't difficult, and are better than not linking at all.