This is the mail archive of the java-patches@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: finalize interned strings


I'm checking in this patch.  It fixes a bug Bryce pointed out last
night: Strings which are interned are never removed from the intern
hash table, leading to crashes if they are GC'd and intern is used
again.

2000-04-12  Tom Tromey  <tromey@cygnus.com>

	* java/lang/natString.cc (unintern): Added `obj' argument.
	(intern): Register finalizer for string.
	* java/lang/String.java (unintern): Now static; added obj
	argument.

Tom

Index: java/lang/String.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/String.java,v
retrieving revision 1.5
diff -u -r1.5 String.java
--- String.java	2000/03/07 19:55:26	1.5
+++ String.java	2000/04/12 20:37:50
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -302,6 +302,6 @@
   private native void init (byte[] chars, int hibyte, int offset, int count);
   private native void init (byte[] chars, int offset, int count, String enc)
     throws UnsupportedEncodingException;
-  private native void unintern ();
+  private static native void unintern (Object obj);
   private static native void rehash ();
 }
Index: java/lang/natString.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/natString.cc,v
retrieving revision 1.11
diff -u -r1.11 natString.cc
--- natString.cc	2000/03/07 19:55:26	1.11
+++ natString.cc	2000/04/12 20:37:53
@@ -1,6 +1,6 @@
 // natString.cc - Implementation of java.lang.String native methods.
 
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -153,15 +153,18 @@
   SET_STRING_IS_INTERNED(this);
   strhash_count++;
   *ptr = this;
+  // When string is GC'd, clear the slot in the hash table.
+  _Jv_RegisterFinalizer ((void *) this, unintern);
   return this;
 }
 
 /* Called by String fake finalizer. */
 void
-java::lang::String::unintern()
+java::lang::String::unintern (jobject obj)
 {
   JvSynchronize sync (&StringClass);
-  jstring* ptr = _Jv_StringGetSlot(this);
+  jstring str = reinterpret_cast<jstring> (obj);
+  jstring* ptr = _Jv_StringGetSlot(str);
   if (*ptr == NULL || *ptr == DELETED_STRING)
     return;
   *ptr = DELETED_STRING;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]