Bug 33913 - Invalid IL Code exception caused by invalid string interpolation in thread lambda
Summary: Invalid IL Code exception caused by invalid string interpolation in thread la...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 4.0.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-09-13 23:39 UTC by WillG
Modified: 2015-09-24 04:01 UTC (History)
1 user (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 WillG 2015-09-13 23:39:36 UTC
Xamarin Studio 5.9.6 (build 23)
Mono 4.0.4 (Not an option in your list)

ButtonWithImage has an OnClick Property that sets the handler of the wrapped button.

                        var saveButton = new ButtonWithImage { 
				...
			};
			saveButton.OnClick = DoSelectPlace;

                void DoSelectPlace (object sender, EventArgs e)
		{
                   #execution never gets to here
                }


System.InvalidProgramException: Invalid IL code in RayvMobileApp.AddPage1:DoSelectPlace (object,System.EventArgs): IL_011f: callvirt  0x060004b0


  at Xamarin.Forms.Button.Xamarin.Forms.IButtonController.SendClicked () [0x00020] in <filename unknown>:0
  at Xamarin.Forms.Platform.iOS.ButtonRenderer.OnButtonTouchUpInside (System.Object sender, System.EventArgs eventArgs) [0x0000e] in <filename unknown>:0
  at UIKit.UIControlEventProxy.Activated () [0x00007] in /Users/builder/data/lanes/2055/67572797/source/maccore/src/UIKit/UIControl.cs:34
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2055/67572797/source/maccore/src/UIKit/UIApplication.cs:63
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2055/67572797/source/maccore/src/UIKit/UIApplication.cs:47
  at RayvMobileApp.iOS.Application.Main (System.String[] args) [0x00008] in /Users/Will/Projects/RayvMobileApp/iOS/Main.cs:17
Comment 1 WillG 2015-09-13 23:40:45 UTC
I replaced the composite ButtonWithImage with a standard Forms Button - same result
Comment 2 WillG 2015-09-13 23:44:20 UTC
This triggers the IL exception:
saveButton.Clicked += (sender, e) => {
				DoSelectPlace (null,nul);
			};
only when DoSelectPlace is called.

This works without exception:
saveButton.Clicked += (sender, e) => {
				DisplayAlert ("ok", "ok", "ok");
			};
Comment 3 WillG 2015-09-14 00:01:00 UTC
Ok, tracked down the 'cause':

the event handler contains:
                                Device.BeginInvokeOnMainThread (() => {
                                                        ...
							Console.WriteLine ($" {addingPlace.place_name}");
					});

If I remove the string interpolation (delete the '$') it works.
Interestingly the variable addingPlace does not exist - it was renamed with the Refactor command, but this ignores string occurrences.

If I remove the threading, the compiler gives an error, but with the threading as shown the compiler doesn't pick up that the variable doesn;t exist, and gives the IL exception when the routine is called.

If I either:
- correct the variable name, or
- remove the string interpolation,
then no error
Comment 4 Marek Safar 2015-09-14 04:26:37 UTC
Please provide full repro. I tried to guess your code but following code works for me as expected

using System;

class X
{
	public static void BeginInvokeOnMainThread (Action action)
	{
	}

	public static void Main ()
	{
		BeginInvokeOnMainThread (() => {
        	Console.WriteLine ($" {addingPlace.place_name}");
        });
	}
}
Comment 5 WillG 2015-09-17 19:14:01 UTC
I tried this:

using System;

using Xamarin.Forms;

namespace RayvMobileApp
{
    public class TestFormsPage : ContentPage
    {
        public static void TestHandler (object s, EventArgs e)
        {
            new System.Threading.Thread (new System.Threading.ThreadStart (() => {
                Console.WriteLine ("Thread");
                Device.BeginInvokeOnMainThread (() => {
                    Console.WriteLine ($"TestHandler {addingPlace.place_name}");
                });
            })).Start ();
        }

        public TestFormsPage ()
        {
            var button = new Button { Text = "Hello ContentPage" };
            button.Clicked += TestHandler;
            Content = new StackLayout { 
                Children = {
                    button
                }
            };
        }
    }
}

and

using System;
using Xamarin.Forms;
using Xamarin;

namespace RayvMobileApp
{
    public class App : Xamarin.Forms.Application
    {
        public App ()
        {
            MainPage = new TestFormsPage ();
        }
}

but I can't test as the compiler crashes

Errors:

/Users/Will/Projects/RayvMobileApp/iOS/RayvMobileApp.iOS.csproj (Build) ->
/Library/Frameworks/Mono.framework/Versions/4.0.4/lib/mono/4.5/Microsoft.CSharp.targets (CoreCompile target) ->

	/Library/Frameworks/Mono.framework/Versions/4.0.4/lib/mono/4.5/Microsoft.CSharp.targets: error : Compiler crashed with code: 1.
Comment 6 Marek Safar 2015-09-21 02:49:53 UTC
Works as expected in Mono 4.2. I get

error CS0103: The name `addingPlace' does not exist in the current context
Comment 7 WillG 2015-09-23 18:12:16 UTC
This was Mono 4.0.4

The current released version in XS
Comment 8 Marek Safar 2015-09-24 04:01:31 UTC
Then please try 4.2 beta