Bugzilla – Bug 320517
[ANONYMOUS METHODS] (FIXED) wrong code generation when using anonymous methods
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by flipper98@gmx.net 2006-03-10 11:21:26 MST ---- Please fill in this template when reporting a bug, unless you know what you are doing. Description of Problem: gmcs generates wrong code in certain scenarios where anonymous methods are involved. Steps to reproduce the problem: compile the attached example code. Actual Results: bad code (program crashes) Expected Results: good code :D How often does this happen? always Additional Information: this is the code you should gmcs fed with: using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Program p = new Program(); p.f(false); } public void f(bool anyCondition) { if (anyCondition) { new MethodInvoker( delegate { Console.WriteLine("anonymous code"); }).BeginInvoke(null, null); return; } O o = new O("1", "2", "3"); new MethodInvoker( delegate { Console.WriteLine(o.AnyMethod()); }).BeginInvoke(null, null); } private delegate void MethodInvoker(); private class O { public O(params string[] args) { // ... } public string AnyMethod() { return ""; } } } } this is the code of function f() actually produced by gmcs (decompiled with reflector): public void f(bool anyCondition) { Program.<>AnonHelp<0> p<>1; if (anyCondition) { p<>1 = new Program.<>AnonHelp<0>(); // <-- NOOO, dear gmcs, thats the wrong place! ;) new Program.MethodInvoker(p<>1.<#AnonymousMethod>0).BeginInvoke(null, null); } else { string[] textArray1 = new string[] { "1", "2", "3" }; p<>1.<0:o> = new Program.O(textArray1); new Program.MethodInvoker(p<>1.<#AnonymousMethod>1).BeginInvoke(null, null); } } ---- Additional Comments From zulu99@gmx.net 2006-03-11 08:51:39 MST ---- ...and here is a testcase with irrelevant code removed: using System; class Program { static void Main(string[] args) { Program p = new Program(); p.f(false); } public void f(bool anyCondition) { if (anyCondition) { MyDelegate d1 = delegate { Console.WriteLine("d1"); }; d1(); } string s = "blah"; MyDelegate d2 = delegate { Console.WriteLine(s); }; d2(); } private delegate void MyDelegate(); } ---- Additional Comments From miguel@ximian.com 2006-03-13 19:44:23 MST ---- Am looking into this. The problem seems to be that we have a "host" set when we should not have one: Host=/tmp/yu.cs(15,17): am=/tmp/yu.cs(25,17): Thats inside AddLocal. What this does is it makes the methods share the same variable definition (s), but that is wrong. ---- Additional Comments From martin@ximian.com 2006-09-14 11:26:07 MST ---- PEVerifies ok ---- Additional Comments From martin@ximian.com 2006-10-04 18:14:51 MST ---- Fixed in SVN. ---- Additional Comments From martin@ximian.com 2006-10-04 18:18:36 MST ---- Fixed in SVN. Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>fedora core 4</cf_op_sys_details> Unknown operating system other. Setting to default OS "Other".