[Mono-bugs] [Bug 82490][Nor] New - ContextStatic variable initializer doesn't work

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Sun Aug 19 15:55:38 EDT 2007


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 jan.oravec at 6com.sk.

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

--- shadow/82490	2007-08-19 15:55:38.000000000 -0400
+++ shadow/82490.tmp.7207	2007-08-19 15:55:38.000000000 -0400
@@ -0,0 +1,148 @@
+Bug#: 82490
+Product: Mono: Runtime
+Version: 1.2
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: remoting
+AssignedTo: lluis at ximian.com                            
+ReportedBy: jan.oravec at 6com.sk               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: ContextStatic variable initializer doesn't work
+
+The per-context variable, like:
+
+[ContextStatic]
+static int foo = 10;
+
+is not initialized to given value. I would expect it to initialize to 10 in
+all contexts (like both static and non-static variables work). It appears,
+that the initializer is called only first time the variable is used. This
+is clearly shown with this example:
+
+using System;
+using System.Runtime.Remoting.Contexts;
+
+
+class GeneratorAttribute : ContextAttribute
+{
+  public GeneratorAttribute ()
+    : base ("generator")
+  {
+  }
+}
+
+[ Generator () ]
+class A : ContextBoundObject
+{
+  public void Foo ()
+  {
+    B.Current ().Foo ();
+  }
+}
+
+class B
+{
+  [ ContextStatic ]
+  static B current = new B ();
+  
+  static int foo = 8;
+  int goo;
+  
+  public static B Current ()
+  {
+    if (current == null)
+    {
+      System.Console.WriteLine ("not initialized?");
+      current = new B ();
+    }
+    return current;
+  }
+  
+  public B ()
+  {
+    this.goo = foo++;
+    System.Console.WriteLine ("C foo:{0} goo:{1} ctxid:{2}", foo, goo,
+System.Threading.Thread.CurrentContext.ContextID);
+  }
+  
+  
+  public void Foo ()
+  {
+    System.Console.WriteLine ("F foo:{0} goo:{1} ctxid:{2}", foo, goo,
+System.Threading.Thread.CurrentContext.ContextID);
+  }
+}
+
+
+class Foo
+{
+  public static void Main ()
+  {
+    A a1 = new A ();
+    A a2 = new A ();
+    A a3 = new A ();
+    
+    a1.Foo ();
+    a2.Foo ();
+    a3.Foo ();
+    a1.Foo ();
+    a2.Foo ();
+    a3.Foo ();
+    
+    B.Current ().Foo ();
+  }
+}
+
+
+The output is:
+C foo:1 goo:0 ctxid:1
+F foo:8 goo:0 ctxid:1
+not initialized?
+C foo:9 goo:8 ctxid:2
+F foo:9 goo:8 ctxid:2
+not initialized?
+C foo:10 goo:9 ctxid:3
+F foo:10 goo:9 ctxid:3
+F foo:10 goo:0 ctxid:1
+F foo:10 goo:8 ctxid:2
+F foo:10 goo:9 ctxid:3
+not initialized?
+C foo:11 goo:10 ctxid:0
+F foo:11 goo:10 ctxid:0
+
+
+Notice, that the first time when B's constructor is called (in non-zero
+context), even static variable 'foo' is not initialized. All next requests
+for 'current' in contexts 2 and 3 aren't calling initializer. Even context
+0 is not initialized.
+
+I would expect output like:
+C foo:9 goo:8 ctxid:1
+F foo:9 goo:8 ctxid:1
+C foo:10 goo:9 ctxid:2
+F foo:10 goo:9 ctxid:2
+C foo:11 goo:10 ctxid:3
+F foo:11 goo:10 ctxid:3
+F foo:11 goo:8 ctxid:1
+F foo:11 goo:9 ctxid:2
+F foo:11 goo:10 ctxid:3
+C foo:12 goo:11 ctxid:0
+F foo:12 goo:11 ctxid:0
+
+
+Also, I think the test mono/mono/tests/context-static.cs is wrong:
+
+                if (T.var != 0) return true;
+
+should probably be
+
+                if (T.var != 5) return true;
+
+as var has initializer var = 5


More information about the mono-bugs mailing list