7072167: The "root" field in BufferStrategyPaintManager leaks memory
authorrupashka
Wed, 05 Oct 2011 18:21:23 +0400
changeset 10690 4e232ff8b640
parent 10689 080571e66cc6
child 10691 50cda9bfed7e
7072167: The "root" field in BufferStrategyPaintManager leaks memory Reviewed-by: alexp
jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java
--- a/jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java	Tue Sep 27 13:38:29 2011 +0400
+++ b/jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java	Wed Oct 05 18:21:23 2011 +0400
@@ -24,20 +24,17 @@
  */
 package javax.swing;
 
-import java.applet.Applet;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
 import java.lang.reflect.*;
 import java.lang.ref.WeakReference;
-import java.security.AccessController;
 import java.util.*;
 
 import com.sun.java.swing.SwingUtilities3;
 
 import sun.awt.SubRegionShowable;
 import sun.java2d.SunGraphics2D;
-import sun.security.action.GetPropertyAction;
 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
 import sun.awt.SunToolkit;
 import sun.util.logging.PlatformLogger;
@@ -120,10 +117,6 @@
      */
     private JComponent rootJ;
     /**
-     * Parent Applet/Window for the current paint/copyArea
-     */
-    private Container root;
-    /**
      * Location of component being painted relative to root.
      */
     private int xOffset;
@@ -278,7 +271,9 @@
     public boolean paint(JComponent paintingComponent,
                          JComponent bufferComponent, Graphics g,
                          int x, int y, int w, int h) {
-        if (prepare(paintingComponent, true, x, y, w, h)) {
+        Container root = fetchRoot(paintingComponent);
+
+        if (prepare(paintingComponent, root, true, x, y, w, h)) {
             if ((g instanceof SunGraphics2D) &&
                     ((SunGraphics2D)g).getDestination() == root) {
                 // BufferStrategy may have already constrained the Graphics. To
@@ -319,7 +314,9 @@
         //
         // If the buffer isn't in sync there is no point in doing a copyArea,
         // it has garbage.
-        if (prepare(c, false, 0, 0, 0, 0) && bufferInfo.isInSync()) {
+        Container root = fetchRoot(c);
+
+        if (prepare(c, root, false, 0, 0, 0, 0) && bufferInfo.isInSync()) {
             if (clip) {
                 Rectangle cBounds = c.getVisibleRect();
                 int relX = xOffset + x;
@@ -500,14 +497,14 @@
      *
      * @return true if should use buffering per window in painting.
      */
-    private boolean prepare(JComponent c, boolean isPaint, int x, int y,
+    private boolean prepare(JComponent c, Container root, boolean isPaint, int x, int y,
                             int w, int h) {
         if (bsg != null) {
             bsg.dispose();
             bsg = null;
         }
         bufferStrategy = null;
-        if (fetchRoot(c)) {
+        if (root != null) {
             boolean contentsLost = false;
             BufferInfo bufferInfo = getBufferInfo(root);
             if (bufferInfo == null) {
@@ -567,10 +564,10 @@
         return false;
     }
 
-    private boolean fetchRoot(JComponent c) {
+    private Container fetchRoot(JComponent c) {
         boolean encounteredHW = false;
         rootJ = c;
-        root = c;
+        Container root = c;
         xOffset = yOffset = 0;
         while (root != null &&
                (!(root instanceof Window) &&
@@ -597,7 +594,7 @@
                         // heavyweights.  If we didn't do this when we
                         // went to show the descendants of the nested hw
                         // you would see nothing, so, we bail out here.
-                        return false;
+                        return null;
                     }
                 }
             }
@@ -613,11 +610,11 @@
                 // bit tricky with Swing. This gives a good approximation
                 // of the various ways to turn on double buffering for
                 // components.
-                return true;
+                return root;
             }
         }
         // Don't do true double buffering.
-        return false;
+        return null;
     }
 
     /**