Skip to content

Commit ed615e3

Browse files
author
Alexander Zuev
committed
4907798: MEMORY LEAK: javax.swing.plaf.basic.BasicPopupMenuUI$MenuKeyboardHelper
Reviewed-by: psadhukhan, serb
1 parent 362feaa commit ed615e3

File tree

3 files changed

+216
-1
lines changed

3 files changed

+216
-1
lines changed

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicPopupMenuUI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1226,6 +1226,7 @@ public void stateChanged(ChangeEvent ev) {
12261226
// menu hidden -- return focus to where it had been before
12271227
// and uninstall menu keybindings
12281228
removeItems();
1229+
menuInputMap = null;
12291230
} else {
12301231
if (popup != lastPopup) {
12311232
receivedKeyPressed = false;

src/java.desktop/windows/classes/com/sun/java/swing/plaf/windows/WindowsPopupMenuUI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public void stateChanged(ChangeEvent ev) {
108108
WindowsGraphicsUtils.repaintMnemonicsInWindow(win);
109109
}
110110
}
111+
repaintRoot = null;
111112
} else {
112113
Component c = (Component)path[0];
113114
if (c instanceof JPopupMenu) c = ((JPopupMenu)c).getInvoker();
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4907798
27+
* @key headful
28+
* @summary Check for memory leak in menu subsystem
29+
* @run main/othervm -Xmx8m PopupReferenceMemoryLeak
30+
*/
31+
32+
import javax.swing.AbstractAction;
33+
import javax.swing.JFrame;
34+
import javax.swing.JMenu;
35+
import javax.swing.JMenuBar;
36+
import javax.swing.JMenuItem;
37+
import javax.swing.SwingUtilities;
38+
import javax.swing.UIManager;
39+
import javax.swing.UnsupportedLookAndFeelException;
40+
import javax.swing.WindowConstants;
41+
import java.awt.BorderLayout;
42+
import java.awt.Robot;
43+
import java.awt.event.ActionEvent;
44+
import java.awt.event.InputEvent;
45+
import java.awt.event.KeyEvent;
46+
import java.lang.ref.WeakReference;
47+
import java.util.ArrayList;
48+
49+
import static javax.swing.UIManager.getInstalledLookAndFeels;
50+
51+
public class PopupReferenceMemoryLeak {
52+
static volatile WeakReference referenceToFrame1;
53+
static JFrame frame1, frame2;
54+
55+
public static void main(String[] args) throws Exception {
56+
Robot robot = new Robot();
57+
robot.setAutoDelay(200);
58+
for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
59+
String lafName = laf.getName();
60+
System.out.println("Testing LaF: " + lafName);
61+
if (lafName == null || lafName.startsWith("Mac OS X")) {
62+
// Aqua Look and Feel uses system menus we can't really test it
63+
continue;
64+
}
65+
setLookAndFeel(laf);
66+
PopupReferenceMemoryLeak newTest = new PopupReferenceMemoryLeak();
67+
SwingUtilities.invokeAndWait(newTest::createUI);
68+
try {
69+
boolean passed = false;
70+
robot.waitForIdle();
71+
Thread.sleep(2000);
72+
robot.mouseMove(200, 200);
73+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
74+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
75+
robot.keyPress(KeyEvent.VK_F10);
76+
robot.keyRelease(KeyEvent.VK_F10);
77+
robot.keyPress(KeyEvent.VK_F);
78+
robot.keyRelease(KeyEvent.VK_F);
79+
robot.keyPress(KeyEvent.VK_C);
80+
robot.keyRelease(KeyEvent.VK_C);
81+
robot.waitForIdle();
82+
Thread.sleep(2000);
83+
robot.mouseMove(600, 200);
84+
robot.waitForIdle();
85+
Thread.sleep(2000);
86+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
87+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
88+
89+
// Workaround for Linux issues when sometimes there
90+
// is a ref to last opened frame from native code
91+
JFrame frame3 = new JFrame("Workaround");
92+
frame3.setSize(100, 100);
93+
frame3.setLocation(0,0);
94+
frame3.setVisible(true);
95+
Thread.sleep(1000);
96+
frame3.setVisible(false);
97+
frame3.dispose();
98+
99+
// Force GC three times to see if it clears the old frame
100+
for (int i = 0; i < 3; i++) {
101+
try {
102+
ArrayList gc = new ArrayList();
103+
while (true) {
104+
gc.add(new int[100000]);
105+
}
106+
} catch (Throwable ignore) {
107+
}
108+
robot.waitForIdle();
109+
Thread.sleep(1000);
110+
if (referenceToFrame1.get() == null) {
111+
// Frame was released
112+
passed = true;
113+
break;
114+
}
115+
}
116+
if (!passed) {
117+
robot.waitForIdle();
118+
robot.keyPress(KeyEvent.VK_F10);
119+
robot.keyRelease(KeyEvent.VK_F10);
120+
robot.keyPress(KeyEvent.VK_T);
121+
robot.keyRelease(KeyEvent.VK_T);
122+
robot.keyPress(KeyEvent.VK_M);
123+
robot.keyRelease(KeyEvent.VK_M);
124+
robot.waitForIdle();
125+
Thread.sleep(2000);
126+
for (int i = 0; i < 5; i++) {
127+
try {
128+
ArrayList gc = new ArrayList();
129+
while (true) {
130+
gc.add(new int[100000]);
131+
}
132+
} catch (Throwable ignore) {
133+
}
134+
robot.waitForIdle();
135+
Thread.sleep(1000);
136+
if (referenceToFrame1.get() == null) {
137+
// Frame was released
138+
throw new RuntimeException("Frame cleared only after menu activated on frame2");
139+
}
140+
}
141+
throw new RuntimeException("Test finished but menu has not cleared the reference!");
142+
}
143+
} catch (Exception re) {
144+
throw new RuntimeException(re.getLocalizedMessage());
145+
} finally {
146+
if (frame2 != null) {
147+
frame2.setVisible(false);
148+
frame2.dispose();
149+
}
150+
}
151+
}
152+
}
153+
154+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
155+
try {
156+
UIManager.setLookAndFeel(laf.getClassName());
157+
} catch (UnsupportedLookAndFeelException ignored) {
158+
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
159+
} catch (Exception e) {
160+
throw new RuntimeException(e);
161+
}
162+
}
163+
164+
165+
public void createUI() {
166+
frame1 = new JFrame("Main test window");
167+
JMenuBar menuBar1 = new JMenuBar();
168+
JMenu file1 = new JMenu("File");
169+
file1.setMnemonic('f');
170+
JMenuItem close1 = new JMenuItem("Close");
171+
close1.setMnemonic('c');
172+
close1.addActionListener(new FrameCloser(frame1));
173+
file1.add(close1);
174+
menuBar1.add(file1);
175+
frame1.setJMenuBar(menuBar1);
176+
frame1.setSize(200, 200);
177+
frame1.setLocation(100, 100);
178+
frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
179+
frame1.setVisible(true);
180+
referenceToFrame1 = new WeakReference(frame1);
181+
frame1 = null;
182+
183+
frame2 = new JFrame("Secondary");
184+
JMenuBar menuBar2 = new JMenuBar();
185+
JMenu test = new JMenu("Test");
186+
test.setMnemonic('T');
187+
JMenuItem memoryTest = new JMenuItem("Memory");
188+
memoryTest.setMnemonic('M');
189+
test.add(memoryTest);
190+
menuBar2.add(test);
191+
frame2.setJMenuBar(menuBar2);
192+
frame2.setLayout(new BorderLayout());
193+
frame2.setSize(200, 200);
194+
frame2.setLocation(500, 100);
195+
frame2.setVisible(true);
196+
}
197+
198+
class FrameCloser extends AbstractAction {
199+
JFrame frame;
200+
public FrameCloser(JFrame f) {
201+
this.frame = f;
202+
}
203+
204+
@Override
205+
public void actionPerformed(ActionEvent e) {
206+
if (frame != null) {
207+
frame.setVisible(false);
208+
frame.dispose();
209+
this.frame = null;
210+
}
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)