RFR: 8376492: NullPointer in ContextMenu sub-menu when graphic and style classes are changed while the menu is open [v2]
Dmitry Markov
dmarkov at openjdk.org
Mon Feb 23 16:18:50 UTC 2026
On Tue, 17 Feb 2026 08:20:45 GMT, eduardsdv <duke at openjdk.org> wrote:
>> Dmitry Markov has updated the pull request incrementally with one additional commit since the last revision:
>>
>> Remove previous ItemStyleClassListener
>
> modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/ContextMenuContent.java line 1299:
>
>> 1297: };
>> 1298: item.getStyleClass().addListener(itemStyleClassListener);
>> 1299: item.getProperties().put(ITEM_STYLE_CLASS_LISTENER, itemStyleClassListener);
>
> I think the reason for the issue is, that the listener ``itemStyleClassListener`` is created and added each time the ``createChildren()`` method is called. This happens once in the constructor and each time the ``graphicProperty()`` is changed (see the lines 1188-1191).
>
> So, after some changes of ``graphicProperty()`` we have a number of listeners added to the ``item.getStyleClass()``, but only last one will be removed when ``dispose()`` is called.
> The ``dispose()`` method also nulls the ``label``. Therefore, if the ``item.getStyleClass()`` is changed afterwards, the listeners that have not been removed will attempt to update the **null**-label.
>
> Therefore, I think, we not only have a NullPointerException but also a memory leak.
>
> What about removing the old listener when a new one is created and added to the ``item.getStyleClass()`` method, as is done in the ``dispose()`` method?
>
>
> ListChangeListener<String> previousItemStyleClassListener = (ListChangeListener<String>)item.getProperties().put(ITEM_STYLE_CLASS_LISTENER, itemStyleClassListener);
> if (previousItemStyleClassListener!= null) {
> item.getStyleClass().removeListener(previousItemStyleClassListener);
> }
You are right, there is a memory leak: I can see the number of `itemStyleClassListener` is growing anytime when `createChildred()` is called.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/2075#discussion_r2841731216
More information about the openjfx-dev
mailing list