2

I am working with some legacy code. During execution, that code creates multiple windows and disposes them. However, they are still reachable and i.e. calling java.awt.Window.getWindows() returns a large array of java.awt.Window, javax.swing.JFrame, javax.swing.JDialog and the like, all of which have their name starting with "dead-". This now increasingly becomes a problem.

So here are my questions: Am I correct, that the 'dead-' prefix of the name is created by the System? What can I do to properly dispose old windows such that they are no longer reachable and returned by java.awt.Window.getWindows()? Thanks a lot!

roesslerj
  • 2,453
  • 5
  • 29
  • 43
  • 4
    Can you provide a piece of code showing how the legacy code currently create / dispose the windows. – fluminis Jul 22 '14 at 13:14
  • 1
    Top-level conbtainers aren't GC'ed, then all are accessible untill current JVM exists, [more here for example](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime), result should be to reuse reasonable numbers of Containers (reduced to max number of container that can be visible on the screen at one time) – mKorbel Jul 22 '14 at 13:20
  • those containers aren't dead, only containers that returns false from isDisplayable – mKorbel Jul 22 '14 at 13:21
  • there are accesible all JOptionPanes, but by casting from its parent - JDialog – mKorbel Jul 22 '14 at 13:23
  • See also this related [Q&A](http://stackoverflow.com/q/6309407/230513). – trashgod Jul 22 '14 at 15:10

1 Answers1

1

I found the following code inside the project:

private void disposeWindow(final java.awt.Window window) {
    SwingEnvironment.runOnEventDispatchingThread(new Runnable() {
        @Override
        public void run() {
            window.dispose();
            window.setVisible(false);
            window.removeNotify();

            String oldName = window.getName();
            if (oldName == null) {
                oldName = "unnamed";
            }

            if (!oldName.startsWith("dead-")) {
                window.setName("dead-" + oldName);
            }
        }
    });
}

So this is the reason the windows where named 'dead-'.

However they were properly disposed and still showed up in the Windows Array that is returned by the java.awt.Window.getWindows(). To get rid of them, I had to create a separate ThreadGroup and create a separate AppContext via SunToolkit.createNewAppContext();. Disposing that AppContext also disposed the Windows properly in my case. See also http://kingsfleet.blogspot.de/2009/10/how-to-have-more-than-one-instance-of.html.

roesslerj
  • 2,453
  • 5
  • 29
  • 43