Exemplo n.º 1
0
 public void JavaReferencedInstanceSurvivesCollection()
 {
     Console.WriteLine("JavaReferencedInstanceSurvivesCollection");
     using (var t = new JniType("java/lang/Object")) {
         var oldHandle = IntPtr.Zero;
         var array     = new JavaObjectArray <JavaObject> (1);
         FinalizerHelpers.PerformNoPinAction(() => {
             var v     = new JavaObject();
             oldHandle = v.PeerReference.Handle;
             array [0] = v;
         });
         JniEnvironment.Runtime.ValueManager.CollectPeers();
         GC.WaitForPendingFinalizers();
         GC.WaitForPendingFinalizers();
         var first = array [0];
         Assert.IsNotNull(JniRuntime.CurrentRuntime.ValueManager.PeekValue(first.PeerReference));
         var f = first.PeerReference;
         var o = (JavaObject)JniRuntime.CurrentRuntime.ValueManager.GetValue(ref f, JniObjectReferenceOptions.Copy);
         Assert.AreSame(first, o);
         if (oldHandle != o.PeerReference.Handle)
         {
             Console.WriteLine("Yay, object handle changed; value survived a GC!");
         }
         else
         {
             Console.WriteLine("What is this, Android pre-ICS?!");
         }
         o.Dispose();
         array.Dispose();
     }
 }
Exemplo n.º 2
0
        public void CrossThreadSharingRequresRegistration()
        {
            JavaObject o = null;

            FinalizerHelpers.PerformNoPinAction(() => {
                o = new JavaObject();
            });
            o.ToString();
            o.Dispose();
        }
Exemplo n.º 3
0
        public void Dispose_Finalized()
        {
            var d = false;
            var f = false;

            FinalizerHelpers.PerformNoPinAction(() => {
                FinalizerHelpers.PerformNoPinAction(() => {
                    var v = new JavaDisposedObject(() => d = true, () => f = true);
                    GC.KeepAlive(v);
                });
                JniEnvironment.Runtime.ValueManager.CollectPeers();
            });
            JniEnvironment.Runtime.ValueManager.CollectPeers();
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(d);
            Assert.IsTrue(f);
        }
Exemplo n.º 4
0
        public void UnreferencedInstanceIsCollected()
        {
            JniObjectReference oldHandle = new JniObjectReference();
            WeakReference      r         = null;

            FinalizerHelpers.PerformNoPinAction(() => {
                var v     = new JavaObject();
                oldHandle = v.PeerReference.NewWeakGlobalRef();
                r         = new WeakReference(v);
            });
            JniEnvironment.Runtime.ValueManager.CollectPeers();
            GC.WaitForPendingFinalizers();
            GC.WaitForPendingFinalizers();
            Assert.IsFalse(r.IsAlive);
            Assert.IsNull(r.Target);
            Assert.IsNull(JniRuntime.CurrentRuntime.ValueManager.PeekValue(oldHandle));
            JniObjectReference.Dispose(ref oldHandle);
        }