Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
            }