static void ValidateNonComWrappers()
        {
            Console.WriteLine($"Running {nameof(ValidateNonComWrappers)}...");

            (WeakReference, IntPtr) GetWeakReference()
            {
                IntPtr objRaw = WeakReferenceNative.CreateWeakReferencableObject();
                var    obj    = Marshal.GetObjectForIUnknown(objRaw);

                return(new WeakReference(obj), objRaw);
            }

            bool HasTarget(WeakReference wr)
            {
                return(wr.Target != null);
            }

            var(weakRef, nativeRef) = GetWeakReference();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // A weak reference to an RCW wrapping an IWeakReference created throguh the built-in system
            // should stay alive even after the RCW dies
            Assert.IsFalse(weakRef.IsAlive);
            Assert.IsTrue(HasTarget(weakRef));

            // Release the last native reference.
            Marshal.Release(nativeRef);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // After all native references die and the RCW is collected, the weak reference should be dead and stay dead.
            Assert.IsNull(weakRef.Target);
        }
        private static IntPtr SetWeakReferenceTarget(WeakReference <WeakReferenceableWrapper> wr, TestComWrappers cw)
        {
            IntPtr objRaw = WeakReferenceNative.CreateWeakReferencableObject();
            var    obj    = (WeakReferenceableWrapper)cw.GetOrCreateObjectForComInstance(objRaw, CreateObjectFlags.None);

            wr.SetTarget(obj);
            ValidateWeakReferenceState(wr, expectedIsAlive: true, cw);
            return(objRaw);
        }
        private static (WeakReference <WeakReferenceableWrapper>, IntPtr) GetWeakReference(TestComWrappers cw)
        {
            IntPtr objRaw = WeakReferenceNative.CreateWeakReferencableObject();
            var    obj    = (WeakReferenceableWrapper)cw.GetOrCreateObjectForComInstance(objRaw, CreateObjectFlags.None);
            var    wr     = new WeakReference <WeakReferenceableWrapper>(obj);

            ValidateWeakReferenceState(wr, expectedIsAlive: true, cw);
            return(wr, objRaw);
        }
示例#4
0
            static (WeakReference <WeakReferencableWrapper>, IntPtr) GetWeakReference()
            {
                var cw = new TestComWrappers();

                IntPtr objRaw = WeakReferenceNative.CreateWeakReferencableObject();

                var obj = (WeakReferencableWrapper)cw.GetOrCreateObjectForComInstance(objRaw, CreateObjectFlags.None);

                return(new WeakReference <WeakReferencableWrapper>(obj), objRaw);
            }
示例#5
0
            static (WeakReference <WeakReferencableWrapper>, IntPtr) GetWeakReference()
            {
                var cw = new TestComWrappers();

                IntPtr objRaw = WeakReferenceNative.CreateWeakReferencableObject();

                var obj = (WeakReferencableWrapper)cw.GetOrCreateObjectForComInstance(objRaw, CreateObjectFlags.None);

                // The returned WeakReferencableWrapper from ComWrappers takes ownership
                // of the ref returned from CreateWeakReferencableObject.
                // Call Marshal.AddRef to ensure that objRaw owns a reference.
                Marshal.AddRef(objRaw);

                return(new WeakReference <WeakReferencableWrapper>(obj), objRaw);
            }