Bug 12544 - Anonymous delegates get corrupted or null values for variables
Summary: Anonymous delegates get corrupted or null values for variables
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
: 12901 ()
Depends on:
Blocks:
 
Reported: 2013-06-04 15:40 UTC by Curtis Wensley
Modified: 2013-06-26 14:19 UTC (History)
4 users (show)

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


Attachments
Reproduces the problem (2.06 KB, application/zip)
2013-06-04 15:40 UTC, Curtis Wensley
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 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 Curtis Wensley 2013-06-04 15:40:34 UTC
Created attachment 4068 [details]
Reproduces the problem

As of Mono 3.0.7+ (tested up to mono 3.0.10), using anonymous delegates in a switch/case statement causes variables outside the delegate to be null or incorrect when used inside the delegate. This appears to be related to having variables within a case block that are used within the anonymous delegate.

An example is attached to reproduce the problem. This problem does not appear in mono 3.0.6.

When running the example, the expected output is:

FirstEvent: TestAnonSwitch.MyClass

The actual result is:

System.NullReferenceException: Object reference not set to an instance of an object
  at TestAnonSwitch.Tester+<Test>c__AnonStorey0.<>m__0 (System.Object sender, System.EventArgs e) [0x00001] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:29 
  at TestAnonSwitch.MyClass.Trigger () [0x0000c] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:13 
  at TestAnonSwitch.Tester.Test () [0x00091] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:42 
  at TestAnonSwitch.MainClass.Main (System.String[] args) [0x00011] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:54 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at TestAnonSwitch.Tester+<Test>c__AnonStorey0.<>m__0 (System.Object sender, System.EventArgs e) [0x00001] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:29 
  at TestAnonSwitch.MyClass.Trigger () [0x0000c] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:13 
  at TestAnonSwitch.Tester.Test () [0x00091] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:42 
  at TestAnonSwitch.MainClass.Main (System.String[] args) [0x00011] in /Users/curtis/Projects/test/TestAnonSwitch/TestAnonSwitch/Program.cs:54
Comment 1 Rodrigo Kumpera 2013-06-11 10:16:20 UTC
Looks like a compiler regression.
Comment 2 Marek Safar 2013-06-26 08:43:58 UTC
Fixed in master
Comment 3 Jonathan Pryor 2013-06-26 14:19:48 UTC
*** Bug 12901 has been marked as a duplicate of this bug. ***