Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use GC handles to marshal monitor event data.

  • Loading branch information...
commit 01afc7fc8c27ceb5a62a1636d2fbff3a0bbb6187 1 parent 415a76a
@jgoz jgoz authored
View
11 src/ZeroMQ/Interop/MonitorEventData.cs
@@ -1,18 +1,15 @@
namespace ZeroMQ.Interop
{
- using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
internal struct MonitorEventData
{
public int Event;
- public IntPtr Addr;
- public int Value;
- public string Address
- {
- get { return Marshal.PtrToStringAnsi(Addr); }
- }
+ [MarshalAs(UnmanagedType.LPStr)]
+ public string Address;
+
+ public int Value;
}
}
View
7 src/ZeroMQ/Monitoring/ZmqMonitor.cs
@@ -126,7 +126,6 @@ public void Start()
int structSize = Marshal.SizeOf(typeof(MonitorEventData));
var buffer = new byte[structSize];
- var ptr = Marshal.AllocHGlobal(structSize);
var pollingInterval = TimeSpan.FromMilliseconds(PollingIntervalMsec);
IsRunning = true;
@@ -139,13 +138,13 @@ public void Start()
continue;
}
- Marshal.Copy(buffer, 0, ptr, structSize);
- var eventData = (MonitorEventData)Marshal.PtrToStructure(ptr, typeof(MonitorEventData));
+ var pinnedBytes = GCHandle.Alloc(buffer, GCHandleType.Pinned);
+ var eventData = (MonitorEventData)Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject(), typeof(MonitorEventData));
+ pinnedBytes.Free();
OnMonitor(ref eventData);
}
- Marshal.FreeHGlobal(ptr);
_socket.Disconnect(_endpoint);
}
Please sign in to comment.
Something went wrong with that request. Please try again.