Hold instances of DiskCache with weak refs to allow early cleanup.

Review by: bobv (TBR)

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@5170 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/util/DiskCache.java b/dev/core/src/com/google/gwt/dev/util/DiskCache.java
index c1bd2b5..76ca9db 100644
--- a/dev/core/src/com/google/gwt/dev/util/DiskCache.java
+++ b/dev/core/src/com/google/gwt/dev/util/DiskCache.java
@@ -18,6 +18,7 @@
 import java.io.File;

 import java.io.IOException;

 import java.io.RandomAccessFile;

+import java.lang.ref.WeakReference;

 import java.util.ArrayList;

 import java.util.List;

 

@@ -43,16 +44,19 @@
 

   private static class Shutdown implements Runnable {

     public void run() {

-      for (DiskCache diskCache : shutdownList) {

+      for (WeakReference<DiskCache> ref : shutdownList) {

         try {

-          diskCache.finalize();

+          DiskCache diskCache = ref.get();

+          if (diskCache != null) {

+            diskCache.finalize();

+          }

         } catch (Throwable e) {

         }

       }

     }

   }

 

-  private static List<DiskCache> shutdownList;

+  private static List<WeakReference<DiskCache>> shutdownList;

 

   private boolean atEnd = true;

   private RandomAccessFile file;

@@ -64,9 +68,10 @@
       file = new RandomAccessFile(temp, "rw");

       file.setLength(0);

       if (shutdownList == null) {

-        shutdownList = new ArrayList<DiskCache>();

+        shutdownList = new ArrayList<WeakReference<DiskCache>>();

         Runtime.getRuntime().addShutdownHook(new Thread(new Shutdown()));

       }

+      shutdownList.add(new WeakReference<DiskCache>(this));

     } catch (IOException e) {

       throw new RuntimeException("Unable to initialize byte cache", e);

     }