Skip to content

Commit

Permalink
8295806: TableViewSkin: memory leak when changing skin
Browse files Browse the repository at this point in the history
Reviewed-by: aghaisas
  • Loading branch information
Andy Goryachev committed Dec 5, 2022
1 parent 2c18c18 commit bb98d88
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 105 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -77,7 +77,7 @@ public TableViewBehavior(TableView<T> control) {
control.selectionModelProperty().addListener(weakSelectionModelListener);
TableViewSelectionModel<T> sm = control.getSelectionModel();
if (sm != null) {
sm.getSelectedCells().addListener(selectedCellsListener);
sm.getSelectedCells().addListener(weakSelectedCellsListener);
}

// Only add this if we're on an embedded platform that supports 5-button navigation
Expand All @@ -86,8 +86,12 @@ public TableViewBehavior(TableView<T> control) {
}
}

@Override public void dispose() {
if (tlFocus != null) tlFocus.dispose();
@Override
public void dispose() {
if (tlFocus != null) {
tlFocus.dispose();
tlFocus = null;
}
super.dispose();
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -405,6 +405,24 @@ protected void setAnchor(int row, TableColumnBase col) {
private Runnable onFocusLeftCell;
public void setOnFocusLeftCell(Runnable r) { onFocusLeftCell = r; }

@Override
public void dispose() {
onScrollPageUp = null;
onScrollPageDown = null;
onFocusPreviousRow = null;
onFocusNextRow = null;
onSelectPreviousRow = null;
onSelectNextRow = null;
onMoveToFirstCell = null;
onMoveToLastCell = null;
onSelectRightCell = null;
onSelectLeftCell = null;
onFocusRightCell = null;
onFocusLeftCell = null;

super.dispose();
}

public void mousePressed(MouseEvent e) {
// // FIXME can't assume (yet) cells.get(0) is necessarily the lead cell
// ObservableList<? extends TablePositionBase> cells = getSelectedCells();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -60,7 +60,6 @@ public class TableRowSkin<T> extends TableRowSkinBase<T, TableRow<T>, TableCell<
* *
**************************************************************************/

private TableViewSkin<T> tableViewSkin;
private final BehaviorBase<TableRow<T>> behavior;


Expand All @@ -83,13 +82,8 @@ public TableRowSkin(TableRow<T> control) {

// install default input map for the TableRow control
behavior = new TableRowBehavior<>(control);
// control.setInputMap(behavior.getInputMap());

updateTableViewSkin();

registerChangeListener(control.tableViewProperty(), e -> {
updateTableViewSkin();

for (int i = 0, max = cells.size(); i < max; i++) {
Node n = cells.get(i);
if (n instanceof TableCell) {
Expand Down Expand Up @@ -229,16 +223,12 @@ private TableView<T> getTableView() {
return getSkinnable().getTableView();
}

private void updateTableViewSkin() {
// test-only
TableViewSkin<T> getTableViewSkin() {
TableView<T> tableView = getSkinnable().getTableView();
if (tableView != null && tableView.getSkin() instanceof TableViewSkin) {
tableViewSkin = (TableViewSkin)tableView.getSkin();
return (TableViewSkin)tableView.getSkin();
}
return null;
}

// test-only
TableViewSkin<T> getTableViewSkin() {
return tableViewSkin;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -43,6 +43,8 @@
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.input.MouseEvent;

import com.sun.javafx.scene.control.ListenerHelper;
import com.sun.javafx.scene.control.behavior.TableViewBehavior;

/**
Expand Down Expand Up @@ -81,11 +83,12 @@ public TableViewSkin(final TableView<T> control) {

// install default input map for the TableView control
behavior = new TableViewBehavior<>(control);
// control.setInputMap(behavior.getInputMap());

flow.setFixedCellSize(control.getFixedCellSize());
flow.setCellFactory(flow -> createCell());

ListenerHelper lh = ListenerHelper.get(this);

EventHandler<MouseEvent> ml = event -> {
// This ensures that the table maintains the focus, even when the vbar
// and hbar controls inside the flow are clicked. Without this, the
Expand All @@ -96,8 +99,8 @@ public TableViewSkin(final TableView<T> control) {
control.requestFocus();
}
};
flow.getVbar().addEventFilter(MouseEvent.MOUSE_PRESSED, ml);
flow.getHbar().addEventFilter(MouseEvent.MOUSE_PRESSED, ml);
lh.addEventFilter(flow.getVbar(), MouseEvent.MOUSE_PRESSED, ml);
lh.addEventFilter(flow.getHbar(), MouseEvent.MOUSE_PRESSED, ml);

// init the behavior 'closures'
behavior.setOnFocusPreviousRow(() -> onFocusAboveCell());
Expand All @@ -113,7 +116,9 @@ public TableViewSkin(final TableView<T> control) {
behavior.setOnFocusLeftCell(() -> onFocusLeftCell());
behavior.setOnFocusRightCell(() -> onFocusRightCell());

registerChangeListener(control.fixedCellSizeProperty(), e -> flow.setFixedCellSize(getSkinnable().getFixedCellSize()));
lh.addChangeListener(control.fixedCellSizeProperty(), (ev) -> {
flow.setFixedCellSize(getSkinnable().getFixedCellSize());
});

updateItemCount();
}
Expand All @@ -127,12 +132,13 @@ public TableViewSkin(final TableView<T> control) {
**************************************************************************/

/** {@inheritDoc} */
@Override public void dispose() {
super.dispose();

@Override
public void dispose() {
if (behavior != null) {
behavior.dispose();
}

super.dispose();
}

/** {@inheritDoc} */
Expand Down

0 comments on commit bb98d88

Please sign in to comment.