JDK-6480546 : JFrame does not call finalize in all cases
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-10-11
  • Updated: 2011-02-16
  • Resolved: 2006-10-11
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
jdk-1_5_0_08-windows-i586-p.exe
java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03
Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
XP2 SP2 all patches as of 22/09/06

EXTRA RELEVANT SYSTEM CONFIGURATION :
IDE:   Eclipse  Build id: M20060629-1905

A DESCRIPTION OF THE PROBLEM :

In a JFrame window using the X in the top of the window results in the 'finalize' method being called.

BUT.. when I try to close the window via a button, the 'finalize' method doesn't get called and it sits in memory. argh!

Have tested with the following JDK's

Code works with...
j2sdk-1_4_2_11-windows-i586-p.exe
jdk-1_5_0-windows-i586.exe
jdk-1_5_0_04-windows-i586-p.exe
jdk-1_5_0_06-windows-i586-p.exe
jdk-1_5_0_07-windows-i586-p.exe

FAILES WITH....
jdk-1_5_0_08-windows-i586-p.exe

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run attached code.

Click on button marked 'EXIT' and NO finalize statement is printed to system.out.

Click on 'X' button in the top right and it does print out that the finalize method is called.

Retest on previous JDK & it works. i.e. both X & 'EXIT' should print out that the finalize method is called.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
both X & 'EXIT' should print out that the finalize method is called.
ACTUAL -
Click on button marked 'EXIT' and NO finalize statement is printed to system.out.

Click on 'X' button in the top right and it does print out that the finalize method is called.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
None


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class CloseTest extends JFrame {
 
	JButton button;
	String name;
 
	protected void finalize() throws Throwable {
		super.finalize();
		System.out.println("finalize + CloseTest:" + name);
	}
	
	public CloseTest(String name) {
		this.name = name;
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		button = new JButton("EXIT:"+ name);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				endEvent();
			}});
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				end();
			}
		});
		getContentPane().add(button);
		pack();
		setVisible(true);
	}
 
	private void endEvent() {
		dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
	}
	
	private void end() {
		setVisible(false);
		dispose();
	}
	
	public static void main(String args[]) {
		
		new CloseTest("1");
		new CloseTest("2");
		new CloseTest("3");
		new CloseTest("4");
		new CloseTest("5");
		new CloseTest("6");
		new CloseTest("7");
		new CloseTest("8");
		new CloseTest("9");
		new CloseTest("0");
		
		System.out.println("ready");
 
		while(true) {
			try {
				Thread.sleep(500);
				System.gc();
			} catch (InterruptedException e) {
			}
		}
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Go back to the last JDK

jdk-1_5_0_07-windows-i586-p.exe

Release Regression From : 5.0u7
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION This looks like another duplicate of: 6471044 : Memory leak in native cursor code which goes back to: 6351698 : Regression: 4506928 testcase is passing with 142_10-b03 but failing with 142_11-b01 The evidence: * This was introduced in 1.5.0_08, but is fixed in 1.5.0_09b02. * By keeping the cursor out of the JFrame and using the space bar to press the JButton, the test behaves as with 1.5.0_07. * The bug can also be reproduced when the close button ('X') is used to close the frame if you first place the cursor inside the JFrame. So, the bottom line is that this problem is fixed in 1.5.0_09 b02.
11-10-2006