Bug 14290 - Invalid IL code in InvalidProgram.AppDelegate w/ Async & Lambdas
Summary: Invalid IL code in InvalidProgram.AppDelegate w/ Async & Lambdas
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-08-28 12:06 UTC by Kirk Woll
Modified: 2013-09-13 12:35 UTC (History)
2 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 GitHub or Developer Community 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 Kirk Woll 2013-08-28 12:06:12 UTC
The following (Monotouch) code will result in an InvalidProgramException when run:

    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;
        
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            LoadPlayers();
            return true;
        }
        
        private class Model
        {
            public Player SelectedPlayer { get; set; }
        }
        private class Player {}
        private Model model = new Model();
        
        private async void LoadPlayers()  // <-- Remove the async and the bug goes away
        {
            Action<Player> selectPlayer = player => {};
            Func<Action<Player>, Action<Player>> selector = functor => player => 
            {
                functor(player);            // Uncomment this and the bug goes away
                Console.WriteLine(model);   // OR uncomment this and the bug goes away
            };
            selector(selectPlayer);
        }
    }

Produces the exception:

Invalid IL code in InvalidProgram.AppDelegate:<LoadPlayers>m__1 (System.Action`1<InvalidProgram.AppDelegate/Player>): IL_0008: stfld     0x04000009

The SSCCE is as short as I could make it.  Please note the three comments in the code that indicate subtle tweaks that suppress the exception.  Needless to say that in my real program, I need all of these facilities (removing async is not an option, for example).
Comment 1 Kirk Woll 2013-08-28 12:09:18 UTC
Actually, you can remove the "SelectedPlayer" property from the model.  Also, my comments should have said "comment this out and the bug goes away", not "uncomment".
Comment 2 Jason Ausborn 2013-09-12 18:31:00 UTC
While this may not be related to the Invalid IL code exception, you may want to consider using an async Task method. 

I believe that async void methods are mainly meant for making asynchronous event handlers possible. 

More information for the async methods: void, Task, and Task<T> can be found at the link below. 

http://msdn.microsoft.com/en-us/magazine/jj991977.aspx

HTH
Comment 3 Kirk Woll 2013-09-12 18:45:12 UTC
Thanks Jason, indeed I've since worked around my problem.  However, I hope this will eventually be fixed? :)
Comment 4 Marek Safar 2013-09-13 11:50:11 UTC
Already fixed in master
Comment 5 Kirk Woll 2013-09-13 12:35:43 UTC
Nice work! :)