JDK-4859570 : SwingUtilities.sharedOwnerFrame is never disposed
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0,1.4.0,1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-05-07
  • Updated: 2003-08-09
  • Resolved: 2003-08-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

Name: apR10133			Date: 05/07/2003


The peer of SwingUtilities.sharedOwnerFrame instance was created by call
to Window.addNotify(or pack()). This instance lies in static variable of
SwingUtilities and is never been disposed of. Because AWTAutoShutdown sees
that there is alive peer exists it doesn't shut AWT down. This causes the
memory leak in some applications, also it causes Frames and Dialogs to
keep the JVM from exiting once all the frames are closed and disposed
(see bug 4726458).

======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b15
17-09-2004

EVALUATION Name: apR10133 Date: 05/07/2003 We could add a HierarchyListener to each Window that uses sharedOwnerFrame as a Parent, and when the displayability changes the sharedOwnerFrame could be disposed if necessary. This changes needs to fix the AWT bug 4726458. ###@###.### ======================================================================
17-09-2004

WORK AROUND Name: apR10133 Date: 05/07/2003 ======================================================================
17-09-2004

SUGGESTED FIX Name: apR10133 Date: 05/07/2003 ------- SwingUtilities.java ------- *** /tmp/sccs.hNaygA Fri Dec 20 18:04:02 2002 --- SwingUtilities.java Fri Dec 20 13:40:33 2002 *************** *** 13,20 **** --- 13,23 ---- import java.util.Vector; import java.util.Hashtable; + import java.util.Iterator; + import java.util.Collection; import java.lang.reflect.*; + import java.lang.ref.WeakReference; import javax.accessibility.*; import javax.swing.plaf.UIResource; *************** *** 1622,1627 **** --- 1625,1693 ---- private static final Object sharedOwnerFrameKey = new StringBuffer("SwingUtilities.sharedOwnerFrame"); + static public void disposeSharedOwnerFrame() { + Frame frame = getSharedOwnerFrame(); + frame.dispose(); + SwingUtilities.appContextPut(sharedOwnerFrameKey, + null); + } + + static class SharedOwnerFrame extends Frame implements HierarchyListener { + public void addNotify() { + super.addNotify(); + installListeners(); + } + + /** + * Install hierarchy listeners on owned windows to watch for displayability changes + */ + void installListeners() { + Window[] windows = getOwnedWindows(); + for (int ind = 0; ind < windows.length; ind++){ + Window window = windows[ind]; + if (window != null) { + window.removeHierarchyListener(this); + window.addHierarchyListener(this); + } + } + } + + /** + * Watches for displayability changes and disposes shared instance if there are no + * displayable children left. + */ + public void hierarchyChanged(HierarchyEvent e) { + if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) == + HierarchyEvent.DISPLAYABILITY_CHANGED && e.getComponent() != this) + { + boolean readyToDispose = true; + Window[] windows = getOwnedWindows(); + for (int ind = 0; ind < windows.length; ind++) { + Window window = windows[ind]; + if (window != null) { + if (window.isDisplayable()) { + readyToDispose = false; + break; + } + } + } + if (readyToDispose) { + dispose(); + } + } + } + public void show() { + // This frame can never be shown + } + public void dispose() { + try { + getToolkit().getSystemEventQueue(); + super.dispose(); + } catch (Exception e) { + // untrusted code not allowed to dispose + } + } + } /** * Returns a toolkit-private, shared, invisible Frame * to be the owner for JDialogs and JWindows created with *************** *** 1634,1652 **** Frame sharedOwnerFrame = (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey); if (sharedOwnerFrame == null) { ! sharedOwnerFrame = new Frame() { ! public void show() { ! // This frame can never be shown ! } ! public synchronized void dispose() { ! try { ! getToolkit().getSystemEventQueue(); ! super.dispose(); ! } catch (Exception e) { ! // untrusted code not allowed to dispose ! } ! } ! }; SwingUtilities.appContextPut(sharedOwnerFrameKey, sharedOwnerFrame); } --- 1700,1706 ---- Frame sharedOwnerFrame = (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey); if (sharedOwnerFrame == null) { ! sharedOwnerFrame = new SharedOwnerFrame(); SwingUtilities.appContextPut(sharedOwnerFrameKey, sharedOwnerFrame); } ###@###.### ======================================================================
17-09-2004