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

Document potential memory leak with JPopupMenu.show

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      This is the javadoc for instance method JPopupMenu.show:

      ------------------------

      public void show(Component invoker, int x, int y)
      Displays the popup menu at the position x,y in the coordinate space of the component invoker.

      Parameters:
          invoker - the component in whose space the popup menu is to appear
          x - the x coordinate in invoker's coordinate space at which the popup menu is to be displayed
          y - the y coordinate in invoker's coordinate space at which the popup menu is to be displayed

      ------------------------

      We have a GUI application which displays multiple tabbed documents. I discovered that after closing a document, it lingered in memory if a right-click context menu had been used on any component on the tab. The menu instances were created once for the window, and then shared among tabs. It turns out that after you call the above method to show a menu, the component quietly persists as the menu's "invoker" property until it is next changed, or cleared by setInvoker(null).

      Indirectly, via event listeners etc, the invoker component reference was persisting the entire closed document and everything associated with it.

      I later found a SECOND instance of this problem: with a custom LookAndFeel we had a generic context menu installed for all JTextComponents application-wide, with undo/redo/cut/copy/paste/delete. Again, that shared menu would quietly hold a reference to the previously invoked component, which again, via attached event listeners, could potentially persist an arbitrary amount of other stuff from a closed window.

      This was extremely not obvious! I had never really noticed the getInvoker and setInvoker methods of JPopupMenu, and had never imagined the invoker parameter of the show method was being used for anything persistent, just positioning the popup and maybe setting its window owner while the popup was visible.

      The documentation for this method should have some warning about the potential problem:

      >This method also sets this popup menu's invoker property to the invoked component, which persists until it is next changed.
      >@see getInvoker
      >@see setInvoker

      The documentation for the invoker property would also be greatly improved by adding some bit of clarification about what the property actually means. E.g.,

      >For a JPopupMenu instance associated with a JMenu, the invoker property refers to the instance of JMenu. For a JPopupMenu used as a free-standing context menu, the invoker is the comonent most recently passed to #show.
      >@see JMenu#getPopupMenu

      Currently, none of those details are discoverable except by digging in the source code!


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: