[Mono-bugs] [Bug 76948][Nor] Changed - closures don't capture local vars correctly

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Fri Dec 9 08:08:23 EST 2005


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by jonpryor at vt.edu.

http://bugzilla.ximian.com/show_bug.cgi?id=76948

--- shadow/76948	2005-12-08 15:39:29.000000000 -0500
+++ shadow/76948.tmp.21032	2005-12-09 08:08:22.000000000 -0500
@@ -1,14 +1,14 @@
 Bug#: 76948
 Product: Mono: Compilers
 Version: 1.1
-OS: 
+OS: unknown
 OS Details: 
 Status: NEW   
 Resolution: 
-Severity: 
+Severity: Unknown
 Priority: Normal
 Component: C#
 AssignedTo: rharinath at novell.com                            
 ReportedBy: lupus at ximian.com               
 QAContact: mono-bugs at ximian.com
 TargetMilestone: ---
@@ -34,6 +34,23 @@
                 }
         }
 
 }
 
 The program should print 0, 1, 2, .., but it always prints 5.
+
+------- Additional Comments From jonpryor at vt.edu  2005-12-09 08:08 -------
+Just out of curiosity, did you test this with CSC.EXE?  The current
+behavior makes sense to me.
+
+It seems that the `i' for-loop variable will be captured & shared
+across all delegate creations, since logically it's the same variable
+throughout the entire loop.  If you don't want a shared variable to be
+passed to the delegate, you'd need to restrict its scope, e.g.
+
+    for (int i = 0; i < 5; ++i) {
+        int v = i;
+        arr [i] = delegate {doit (v);};
+    }
+
+At least, this is what would make sense to me, as I haven't tested
+this on CSC.EXE either.


More information about the mono-bugs mailing list