public void PinnedBufferIsUnpinnedWhenUnpinnerIsCollected() { UnpinWhenGCedTest.Unpinned = false; var buffer = new byte[] { 1, 2, 3 }; var unpinner = new Unpinner(new UnpinWhenGCedTest(GCHandle.Alloc(buffer, GCHandleType.Pinned))); GC.Collect(3, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); GC.Collect(3, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); Assert.IsTrue(UnpinWhenGCedTest.Unpinned); }
public void PinnedBufferIsNotUnpinnedWhenUnpinnerIsNotCollected() { UnpinWhenGCedTest.Unpinned = false; var buffer = new byte[] { 1, 2, 3 }; var unpinner = new Unpinner(new UnpinWhenGCedTest(GCHandle.Alloc(buffer, GCHandleType.Pinned))); var copy = unpinner; GC.Collect(3, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); GC.Collect(3, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); Assert.IsFalse(UnpinWhenGCedTest.Unpinned); //unpinner is still in scope Console.WriteLine(copy.ToString()); }