From 528f2dc93fc2a9eaf6c8d7c81a712aa4a55a6379 Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Mon, 7 Mar 2022 14:49:58 -0600 Subject: [PATCH 1/3] This seems to prevent the crash --- .../Interop/MessagePropertyVariants.cs | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Messaging/Interop/MessagePropertyVariants.cs b/src/Messaging/Interop/MessagePropertyVariants.cs index aea2134..b8d4486 100644 --- a/src/Messaging/Interop/MessagePropertyVariants.cs +++ b/src/Messaging/Interop/MessagePropertyVariants.cs @@ -6,7 +6,7 @@ namespace MSMQ.Messaging.Interop { - + // definition for tagMQPROPVARIANT @@ -309,8 +309,12 @@ public virtual MQPROPS Lock() newVectorProperties[usedProperties].vt = vt; if (vt == (short)(VT_VECTOR | VT_UI1)) { - if (handles[i] == null) + bool isGcHandle = false; + if (handles[i] == null || handles[i] is GCHandle) + { + isGcHandle = handles[i] is GCHandle; newVectorProperties[usedProperties].caub.cElems = (uint)((byte[])objects[i]).Length; + } else newVectorProperties[usedProperties].caub.cElems = (uint)handles[i]; @@ -342,6 +346,11 @@ public virtual MQPROPS Lock() } handleVectorIdentifiers = GCHandle.Alloc(newVectorIdentifiers, GCHandleType.Pinned); + if (!handleVectorIdentifiers.IsAllocated) + { + Console.WriteLine(); + } + handleVectorProperties = GCHandle.Alloc(newVectorProperties, GCHandleType.Pinned); handleVectorStatus = GCHandle.Alloc(newVectorStatus, GCHandleType.Pinned); vectorIdentifiers = newVectorIdentifiers; @@ -351,6 +360,7 @@ public virtual MQPROPS Lock() reference.propertyIdentifiers = handleVectorIdentifiers.AddrOfPinnedObject(); reference.propertyValues = handleVectorProperties.AddrOfPinnedObject(); reference.status = handleVectorStatus.AddrOfPinnedObject(); + return reference; } @@ -409,7 +419,12 @@ public virtual void Unlock() } else if (vt == VT_LPWSTR || vt == VT_CLSID || vt == (short)(VT_VECTOR | VT_UI1)) { - ((GCHandle)handles[vectorIdentifiers[i] - basePropertyId]).Free(); + object obj = handles[vectorIdentifiers[i] - basePropertyId]; + if (obj is GCHandle) + { + ((GCHandle)obj).Free(); + } + handles[vectorIdentifiers[i] - basePropertyId] = null; } else if (vt == VT_UI1 || vt == VT_I1) @@ -423,9 +438,20 @@ public virtual void Unlock() } - handleVectorIdentifiers.Free(); - handleVectorProperties.Free(); - handleVectorStatus.Free(); + if (handleVectorIdentifiers.IsAllocated) + { + handleVectorIdentifiers.Free(); + } + + if (handleVectorProperties.IsAllocated) + { + handleVectorProperties.Free(); + } + + if (handleVectorStatus.IsAllocated) + { + handleVectorStatus.Free(); + } } [StructLayout(LayoutKind.Sequential)] From e0327a97eb29ca50b122f7bf38f7bb0fe4e7f87b Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Mon, 7 Mar 2022 15:04:40 -0600 Subject: [PATCH 2/3] Remove the unecessary boolean check --- src/Messaging/Interop/MessagePropertyVariants.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Messaging/Interop/MessagePropertyVariants.cs b/src/Messaging/Interop/MessagePropertyVariants.cs index b8d4486..2bc06a8 100644 --- a/src/Messaging/Interop/MessagePropertyVariants.cs +++ b/src/Messaging/Interop/MessagePropertyVariants.cs @@ -309,10 +309,8 @@ public virtual MQPROPS Lock() newVectorProperties[usedProperties].vt = vt; if (vt == (short)(VT_VECTOR | VT_UI1)) { - bool isGcHandle = false; if (handles[i] == null || handles[i] is GCHandle) { - isGcHandle = handles[i] is GCHandle; newVectorProperties[usedProperties].caub.cElems = (uint)((byte[])objects[i]).Length; } else From 6f88f5a6c8ca93346df582d1e91798779116acd4 Mon Sep 17 00:00:00 2001 From: Ben Rush Date: Mon, 7 Mar 2022 15:05:05 -0600 Subject: [PATCH 3/3] Remove console writeline --- src/Messaging/Interop/MessagePropertyVariants.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Messaging/Interop/MessagePropertyVariants.cs b/src/Messaging/Interop/MessagePropertyVariants.cs index 2bc06a8..5724a71 100644 --- a/src/Messaging/Interop/MessagePropertyVariants.cs +++ b/src/Messaging/Interop/MessagePropertyVariants.cs @@ -344,10 +344,6 @@ public virtual MQPROPS Lock() } handleVectorIdentifiers = GCHandle.Alloc(newVectorIdentifiers, GCHandleType.Pinned); - if (!handleVectorIdentifiers.IsAllocated) - { - Console.WriteLine(); - } handleVectorProperties = GCHandle.Alloc(newVectorProperties, GCHandleType.Pinned); handleVectorStatus = GCHandle.Alloc(newVectorStatus, GCHandleType.Pinned);