[Mono-bugs] [Bug 29256][Wis] Changed - Structure not properly marshalled on callback

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
12 Sep 2002 11:11:53 -0000


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 dietmar@ximian.com.

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

--- shadow/29256	Wed Aug 21 13:19:05 2002
+++ shadow/29256.tmp.854	Thu Sep 12 07:11:53 2002
@@ -0,0 +1,120 @@
+Bug#: 29256
+Product: Mono/Runtime
+Version: unspecified
+OS: unknown
+OS Details: RH7.3
+Status: RESOLVED   
+Resolution: FIXED
+Severity: Unknown
+Priority: Wishlist
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: jason@379.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Structure not properly marshalled on callback
+
+Description of Problem:
+Structure values appear to be improperly marshalled on callback function.
+
+
+Steps to reproduce the problem:
+Source code follows:
+ 
+  -----------------------------------------------------------------------
+  FILE: source.c
+  -----------------------------------------------------------------------
+  struct Event {
+    double timestamp;
+  };
+
+  typedef void (*EventHandler)(Event*);
+  EventHandler handler;
+
+  void SetEventHandler(EventHandler eh)
+  {
+    handler = eh;
+  }
+
+  void GenerateEvent();
+  {
+    Event e;
+    e.timestamp = 22.0;
+    handler(&e);
+  }
+  -----------------------------------------------------------------------
+
+  -----------------------------------------------------------------------
+  FILE: CallbackTest.cs
+  -----------------------------------------------------------------------
+  using System;
+  using System.Runtime.InteropServices;
+
+  public class CallbackTest
+  {
+    // --- begin DLL interface ---
+
+    [StructLayout(LayoutKind.Sequential)]
+    public struct Event {
+      public readonly double Timestamp;
+    }
+
+    public delegate void EventHandler(Event e);
+
+    [DllImport("source")]
+    private static extern void SetEventHandler(EventHandler eh);
+
+    [DllImport("source")]
+    private static extern void GenerateEvent();
+
+    // --- end DLL interface ---
+
+    public EventHandler Handlers;
+
+    static void Main()
+    {
+      Handlers += new EventHandler(myHandler);
+      SetEventHandler(Handlers);
+
+      GenerateEvent();
+    }
+
+    private void myHandler(Event e)
+    {
+      Console.WriteLine("Received event, timestamp={0}", e.timestamp);
+    }
+  }
+  -----------------------------------------------------------------------
+
+    
+Actual Results:
+"Received event, timestamp=7.12748"
+
+Expected Results:
+"Received event, timestamp=22.0"
+
+How often does this happen? 
+Every time.
+
+Additional Information:
+
+------- Additional Comments From jason@379.com  2002-08-21 13:19 -------
+Shoot, sorry. That should be 
+   
+    public class Event {
+           ^^^^^
+
+not struct.
+
+
+------- Additional Comments From dietmar@ximian.com  2002-09-12 07:11 -------
+I think this is your bug, not a bug in mono. Event is a value type,
+and value types are marshalled by value:
+
+typedef void (*EventHandler)(Event);
+
+and you must use:
+
+handler (e); to call it (instead of handler(&e)).