Possible WeakReference Bug

Tom Tromey tromey@redhat.com
Mon Nov 18 15:34:00 GMT 2002


>>>>> "Jesse" == Jesse Rosenstock <jmr@ugcs.caltech.edu> writes:

Jesse> It seems that the WeakReference is being enqueued even though
Jesse> the referent is still live and WeakReference.get() returns
Jesse> non-null.

Yes, that's a bug.  Oops.
The spec requires weak and soft references to be cleared before
enqueueing.

Jesse> Also, it seems that the finalizer is never called, is that
Jesse> expected?

Nope, another bug.

The appended patch is a start, but not complete.
It just addresses a couple obvious problems.

I wouldn't be surprised to find other bugs in this area.
The code to implement java.lang.ref is pretty horrible.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* java/lang/ref/natReference.cc (add_to_hash): Look at `copy', not
	`referent'.
	(finalize_referred_to_object): Don't modify `referent' or `copy'
	fields.
	* java/lang/ref/Reference.java (enqueue): Return false if already
	enqueued.

Index: java/lang/ref/Reference.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/ref/Reference.java,v
retrieving revision 1.3
diff -u -r1.3 Reference.java
--- java/lang/ref/Reference.java 22 Jan 2002 22:40:18 -0000 1.3
+++ java/lang/ref/Reference.java 18 Nov 2002 22:26:22 -0000
@@ -1,5 +1,5 @@
 /* java.lang.ref.Reference
-   Copyright (C) 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -185,7 +185,7 @@
    */
   public boolean enqueue() 
   {
-    if (queue != null)
+    if (queue != null && nextOnQueue != null)
       {
 	queue.enqueue(this);
 	queue = null;
Index: java/lang/ref/natReference.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/ref/natReference.cc,v
retrieving revision 1.2
diff -u -r1.2 natReference.cc
--- java/lang/ref/natReference.cc 3 Oct 2001 16:47:02 -0000 1.2
+++ java/lang/ref/natReference.cc 18 Nov 2002 22:26:22 -0000
@@ -1,6 +1,6 @@
 // natReference.cc - Native code for References
 
-/* Copyright (C) 2001  Free Software Foundation
+/* Copyright (C) 2001, 2002  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -165,7 +165,8 @@
   if (3 * hash_count >= 2 * hash_size)
     rehash ();
 
-  jobject referent = the_reference->referent;
+  // Use `copy' here because the `referent' field has been cleared.
+  jobject referent = the_reference->copy;
   object_list *item = find_slot (referent);
   if (item->reference == NULL)
     {
@@ -249,13 +250,7 @@
 	  // If the copy is already NULL then the user must have
 	  // called Reference.clear().
 	  if (ref->copy != NULL)
-	    {
-	      if (w == PHANTOM)
-		ref->referent = ref->copy;
-	      else
-		ref->copy = NULL;
-	      ref->enqueue ();
-	    }
+	    ref->enqueue ();
 
 	  object_list *next = head->next;
 	  _Jv_Free (head);



More information about the Java mailing list