public void VerifyAddPinsObject() { var expected = new string('x', 5); var weakref = new WeakReference(expected); using (var handles = new GCHandleCollection()) { handles.Add(expected); expected = null; RunFullGarbageCollection(); Assert.IsTrue(weakref.IsAlive); } }
public void GCHandleCollectionStress() { for (int i = 0; i < 1000; i++) { using (var handles = new GCHandleCollection()) { for (int j = 0; j < 100; j++) { IntPtr p = handles.Add(new byte[1]); Assert.AreNotEqual(IntPtr.Zero, p); } } } }
public void VerifyDisposeUnpinsObjects() { var expected = new string('x', 5); var weakref = new WeakReference(expected); using (var handles = new GCHandleCollection()) { handles.Add(expected); expected = null; // needed to allow GC to work } GC.Collect(); Assert.IsFalse(weakref.IsAlive); }
/// <summary> /// Gets the NATIVE_indexcolumn structure that represents the object. /// </summary> /// <param name="handles">GC handle collection to add any pinned handles.</param> /// <returns>The NATIVE_indexcolumn structure.</returns> internal NATIVE_INDEX_COLUMN GetNativeIndexColumn(ref GCHandleCollection handles) { NATIVE_INDEX_COLUMN indexColumn = new NATIVE_INDEX_COLUMN(); indexColumn.columnid = this.columnid.Value; indexColumn.relop = (uint)this.relop; indexColumn.grbit = (uint)this.grbit; if (this.pvData != null) { indexColumn.pvData = handles.Add(this.pvData); indexColumn.cbData = (uint)this.pvData.Length; } return(indexColumn); }
public void VerifyDisposeUnpinsObjects() { var expected = new string('x', 5); var weakref = new WeakReference(expected); using (var handles = new GCHandleCollection()) { handles.Add(expected); expected = null; // needed to allow GC to work } RunFullGarbageCollection(); // In DEBUG test code, the objects remain alive for an indeterminate amount of time, for some reason. // Note that they do get collected if a RETAIL test code is used, even if the product code is DEBUG // so it must be something to do with assigning the local variable 'expected' to null and the effect that // it has on garbage collecting weak references to it. #if !DEBUG Assert.IsFalse(weakref.IsAlive); #endif }