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

JMenu's remove method does not unregister accessibleParent

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 5.0
    • client-libs

      FULL PRODUCT VERSION :
      Java 1.5.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

      ADDITIONAL OS VERSION INFORMATION :
      Window XP

      A DESCRIPTION OF THE PROBLEM :
      In the add method of JMenu, the accessibleParent of a JMenuItem is set to be the JMenu. However, the remove method does not unregister the accessibleParent. This causes a memory leak if a reference ot the JMenuItem is kept even if the JMenuItem is removed from the JMenu-- the JMenu cannot be garbage collected, and all other JMenuItems that are parented to the JMenu will also be kept.

      Here is the add method:

      public JMenuItem add(JMenuItem menuItem) {
              AccessibleContext ac = menuItem.getAccessibleContext();
              ac.setAccessibleParent(this);
              ensurePopupMenuCreated();
              return popupMenu.add(menuItem);
       }

      Here is the remove method:

      public void remove(int pos) {
              if (pos < 0) {
                  throw new IllegalArgumentException("index less than zero.");
              }
              if (pos > getItemCount()) {
                  throw new IllegalArgumentException("index greater than the number of items.");
              }
              if (popupMenu != null)
      popupMenu.remove(pos);
          }



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Populate a menu with several menu items (one of which you keep a reference to). Remove all menu items from the menu. The menu and other menu items still will not be garbaged collected because of the 1 that you have a reference to.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Calling remove should unregister the accessibleParent.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Either set the accessibleParent to null or add the menu item (which you are keeping a reference to) to a dummy menu just to cause the accessibleParent to switch.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: