[Mono-bugs] [Bug 79010][Nor] New - Problem with using a generic function from an interface implemented by structure

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Fri Aug 4 05:08:42 EDT 2006


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 dpotapov at gmail.com.

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

--- shadow/79010	2006-08-04 05:08:42.000000000 -0400
+++ shadow/79010.tmp.13977	2006-08-04 05:08:42.000000000 -0400
@@ -0,0 +1,138 @@
+Bug#: 79010
+Product: Mono: Runtime
+Version: 1.1
+OS: 
+OS Details: Debian sarge
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: JIT
+AssignedTo: lupus at ximian.com                            
+ReportedBy: dpotapov at gmail.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Problem with using a generic function from an interface implemented by structure
+
+Description of Problem:
+I got NullReferenceException though the referred object I used was not null.
+The problem happens only with a generic function and only if the interface
+is implemented by a structure. The problem happens only with Mono runtime
+and the same exe file works fine with Microsoft runtime on Windows.
+
+Steps to reproduce the problem:
+The following code snippet demonstrates the problem:
+
+===
+using System;
+using System.Diagnostics;
+
+public interface IMyInStream
+{
+    byte Get();
+}
+
+public class MyInStream : IMyInStream
+{
+    public MyInStream() { }
+    public byte Get() { return 0; }
+}
+
+public interface IMyHandler
+{
+    void Foo(out int v);
+    void Bar<T>(out T v);
+}
+
+struct Handler : IMyHandler
+{
+    Test t;
+
+    public Handler(Test test)
+    {
+        t = test;
+    }
+
+    // This works
+    public void Foo(out int v)
+    {
+        t.GetByte();
+        v = 0;
+    }
+    // and this one does not
+    // the only difference between these two functions is
+    // that the later is generic
+    public void Bar<T>(out T v)
+    {
+        t.GetByte();
+        v = default(T);
+    }
+}
+
+public class Test
+{
+
+    public byte GetByte()
+    {
+        // A strange thing happens here when it is called from Bar<T>
+        // stream is not null and even GetType() returns something for it
+        Debug.Assert(stream != null);
+        Type t = stream.GetType();
+        Debug.Assert(t != null);
+        // however, attempt to call t.ToString() causes NullReferenceException
+        // string s = t.ToString();
+        // as well as attempt to get a byte from the stream
+        return stream.Get();
+    }
+
+    // it is important to have interface, not MyInStream
+    IMyInStream stream;
+
+    public Test()
+    {
+        stream = new MyInStream();
+    }
+
+    // it is important to pass interface, not MyHandler itself
+    private static void Do(IMyHandler h)
+    {
+        int value;
+        h.Foo(out value);
+        Console.WriteLine("value={0}", value);
+        h.Bar(out value);
+        Console.WriteLine("value={0}", value);
+    }
+    public static void Main()
+    {
+        Test test = new Test();
+        Handler handler = new Handler(test);
+        Do(handler);
+    }
+}
+===
+
+Actual Results:
+value=0
+
+Unhandled Exception: System.NullReferenceException: Object reference not
+set to an instance of an object
+in [0x00012] (at /home/dpotapov/cimetrics/bacnet/bacstac.net/bug1.cs:60)
+Test:GetByte ()
+in <0x00019> Handler:Bar[Int32] (System.Int32 v)
+in [0x0001b] (at /home/dpotapov/cimetrics/bacnet/bacstac.net/bug1.cs:77)
+Test:Do (IMyHandler h)
+in [0x00014] (at /home/dpotapov/cimetrics/bacnet/bacstac.net/bug1.cs:84)
+Test:Main ()
+
+
+Expected Results:
+value=0
+value=0
+
+How often does this happen? 
+Always
+
+Additional Information:
+I use Mono 1.1.13.8 installed by Linux Installer for x86


More information about the mono-bugs mailing list