NSMenuFX
NSMenuFX copied to clipboard
Windows menu retains reference to removed stages
I've had a problem recently where the video memory of canvases in my JavaFX stages is not released when I remove the stage and I run out of memory. The problem goes away if I remove (along with a few other things in my code) the use of autoAddWindowMenuItems for the Window menu.
I think the problem is from the following methods in WindowMenuUpdateListener.
addWindowMenuItem adds a reference to the stage in createdMenuItems, but removeWindowMenuItem doesn't remove it. So even though you remove the stage it isn't properly garbage collected as there is still a reference to it.
private void removeWindowMenuItem(Stage stage, Menu menu) {
MenuItem menuItem = createdMenuItems.get(stage);
if (menuItem != null) {
menu.getItems().remove(menuItem);
}
}
private void addWindowMenuItem(Stage stage, Menu menu) {
CheckMenuItem item = new CheckMenuItem(stage.getTitle());
item.setOnAction(event -> stage.toFront());
createdMenuItems.put(stage, item);
menu.getItems().add(item);
}