Пример #1
0
  @Test
  public void testQueuedWeakReference() {
    final boolean[] dealloced = {false};
    ReferenceQueue<? super Object> queue = new ReferenceQueue<Object>();
    for (@AutoreleasePool int i = 0; i < 1; i++) {
      Object referent =
          new Object() {
            public void finalize() {
              dealloced[0] = true;
            }
          };
      weakRef = new WeakReference<Object>(referent, queue);
      assertSame("weakRef.get doesn't return referent", referent, weakRef.get());

      // Remove reference to o, verify it's still available in the reference.
      referent = null;
      assertNotNull("weakRef cleared too soon", weakRef.get());
      assertFalse("referent dealloc'ed too soon", dealloced[0]);
    }

    // Verify weak reference was queued.
    Reference<?> queuedRef = queue.poll();
    assertNotNull("weakRef wasn't queued", queuedRef);

    // Verify weak reference was cleared.
    assertNull("weakRef wasn't cleared", weakRef.get());
    assertTrue("referent wasn't dealloc'ed", dealloced[0]);
  }
Пример #2
0
  @Test
  public void testWeakReference() {
    final boolean[] dealloced = {false};
    for (@AutoreleasePool int i = 0; i < 1; i++) {
      // Create a referent inside this autorelease pool.
      Object referent =
          new Object() {
            public void finalize() {
              dealloced[0] = true;
            }
          };
      weakRef = new WeakReference<Object>(referent);
      assertSame("weakRef get doesn't return referent", referent, weakRef.get());

      // Clear referent ref, verify it's still available in the reference.
      referent = null;
      assertNotNull("weakRef cleared too soon", weakRef.get());
      assertFalse("referent dealloc'ed too soon", dealloced[0]);
    }

    // Verify weak reference was cleared.
    assertNull("weakRef wasn't cleared", weakRef.get());
    assertTrue("referent wasn't dealloc'ed", dealloced[0]);
  }