diff options
Diffstat (limited to 'tests/src/GC/Features/Pinning/PinningOther/PinnedMultiple.cs')
-rw-r--r-- | tests/src/GC/Features/Pinning/PinningOther/PinnedMultiple.cs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/src/GC/Features/Pinning/PinningOther/PinnedMultiple.cs b/tests/src/GC/Features/Pinning/PinningOther/PinnedMultiple.cs new file mode 100644 index 0000000000..3b6472d1b6 --- /dev/null +++ b/tests/src/GC/Features/Pinning/PinningOther/PinnedMultiple.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +// Tests multiple handles for same object + + +using System; +using System.Runtime.InteropServices; + +public class Test +{ + public static int Main() + { + int[] arr = new int[1000]; + GCHandle[] arrhandle = new GCHandle[10000]; // array of handles to the same object + IntPtr[] oldaddress = new IntPtr[10000]; // store old addresses + IntPtr[] newaddress = new IntPtr[10000]; // store new addresses + + for (int i = 0; i < 10000; i++) + { + arrhandle[i] = GCUtil.Alloc(arr, GCHandleType.Pinned); + oldaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]); + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + + for (int i = 0; i < 10000; i++) + { + newaddress[i] = GCUtil.AddrOfPinnedObject(arrhandle[i]); + } + + for (int i = 0; i < 10000; i++) + { + if (oldaddress[i] != newaddress[i]) + { + Console.WriteLine("Test failed"); + return 1; + } + } + + Console.WriteLine("Test passed"); + return 100; + } +} |