Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6386583

Using JPopupMenu.add(Action) gives a memory leak

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Sample program doesn't release memory.
      The bug is similar to bug ID 4193196 except that the fix was not ported to JPopupMenu
      See http://weblogs.java.net/blog/timboudreau/archive/2005/04/writing_memory.html for details.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class MyFrame extends javax.swing.JFrame {
          MenuAction menuAction = new MenuAction();
          public MyFrame() {
              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              setBounds (20, 20, 300, 300);
              getContentPane().addMouseListener (new MouseAdapter() {
                  public void mouseReleased (MouseEvent me) {
                      getPopupMenu().show((Component) me.getSource(), me.getX(), me.getY());
                  }
              });
          }
          
          JPopupMenu getPopupMenu() {
              JPopupMenu menu = new JPopupMenu();
              menu.add (new JMenuItem (menuAction));
              return menu;
          }
          
          static final class MenuAction extends AbstractAction {
              public MenuAction() {
                  putValue (Action.NAME, "Do Something");
              }
              public void actionPerformed (ActionEvent ae) {
                  System.out.println("Action performed");
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      One possible workaround removes the memory leak but removes some functionality.
      Extend JPopupMenu and override JPopupMenu.createActionComponent:
          protected JMenuItem createActionComponent(Action a) {
              JMenuItem mi = new JMenuItem((String)a.getValue(Action.NAME),
                                           (Icon)a.getValue(Action.SMALL_ICON));
              mi.setHorizontalTextPosition(JButton.TRAILING);
              mi.setVerticalTextPosition(JButton.CENTER);
              mi.setEnabled(a.isEnabled());
      return mi;
          }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              svioletsunw Scott Violet (Inactive)
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: