JDK-8245282 : Button/Combo Behavior: memory leak on dispose
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: openjfx14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-05-19
  • Updated: 2020-06-02
  • Resolved: 2020-06-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
openjfx15Fixed
Related Reports
Blocks :  
Blocks :  
Description
The issue turned up in working on JDK-8244531. Failing test case:

    @Test
    public void testButtonBehaviorMemoryLeak() {
        Button control = new Button();
        WeakReference<BehaviorBase<?>> weakRef = new WeakReference<>(createBehavior(control));
        assertNotNull(weakRef.get());
        weakRef.get().dispose();
        attemptGC(weakRef);
        assertNull("behavior must be gc'ed", weakRef.get());
    }
    
Problem missing removal of listener to control's focusedProperty. Actually, the behavior tries to but gets it wrong:

     // in constructor
     getNode().focusedProperty().addListener(this::focusChanged);

    // in dispose
    getNode().focusedProperty().removeListener(this::focusChanged);

the incorrect assumption here is that the lambda being remove is the _same_ instance as the one added (which isn't the case)

Fix is to have a field for the focusListener and add/remove that

    InvalidationListener listener = this::focusChanged;
    ...
     // in constructor
     getNode().focusedProperty().addListener(focusListener);

    // in dispose
    getNode().focusedProperty().removeListener(focusListener);

Same issue with same reason and fix in ComboBoxBaseBehavior - will defer fix until JDK-8244531 is integrated (can then add cross-behavior test)
Comments
Changeset: 853ac78a Author: Jeanette Winzenburg <fastegal@openjdk.org> Committer: Ambarish Rapte <arapte@openjdk.org> Date: 2020-06-02 14:21:35 +0000 URL: https://git.openjdk.java.net/jfx/commit/853ac78a
02-06-2020