Skip to content

Commit 7fabd98

Browse files
committed
ParagraphText: immediately remove listeners from SelectionPath and Caret on dispose instead of waiting for GC (this improves PR FXMisc#779, issue FXMisc#627)
without this change, disposed ParagraphText objects still listen to selection and caret and do some useless stuff
1 parent 3faf6bd commit 7fabd98

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ public void dispose() {
14631463
box.wrapTextProperty().unbind();
14641464
box.graphicFactoryProperty().unbind();
14651465
box.graphicOffset.unbind();
1466+
box.dispose();
14661467

14671468
firstParPseudoClass.unsubscribe();
14681469
lastParPseudoClass.unsubscribe();

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javafx.geometry.Insets;
2323
import javafx.geometry.Point2D;
2424
import javafx.scene.Node;
25-
import javafx.scene.control.IndexRange;
2625
import javafx.scene.layout.Region;
2726
import javafx.scene.paint.Paint;
2827
import javafx.scene.text.TextFlow;
@@ -118,6 +117,10 @@ public final ObservableMap<Selection<PS, SEG, S>, SelectionPath> selectionsPrope
118117
graphicOffset.addListener(obs -> requestLayout());
119118
}
120119

120+
void dispose() {
121+
text.dispose();
122+
}
123+
121124
@Override
122125
public String toString() {
123126
return String.format(

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class ParagraphText<PS, SEG, S> extends TextFlowExt {
6262
FXCollections.observableMap(new HashMap<>(1));
6363
public final ObservableMap<Selection<PS, SEG, S>, SelectionPath> selectionsProperty() { return selections; }
6464

65+
private final ChangeListener<IndexRange> selectionRangeListener;
66+
private final ChangeListener<Integer> caretPositionListener;
67+
6568
// FIXME: changing it currently has not effect, because
6669
// Text.impl_selectionFillProperty().set(newFill) doesn't work
6770
// properly for Text node inside a TextFlow (as of JDK8-b100).
@@ -95,11 +98,11 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
9598
Val<Double> leftInset = Val.map(insetsProperty(), Insets::getLeft);
9699
Val<Double> topInset = Val.map(insetsProperty(), Insets::getTop);
97100

98-
ChangeListener<IndexRange> requestLayout1 = new SelectionRangeChangeListener<>(this);
99-
selections.addListener(new SelectionsSetListener<>(leftInset, requestLayout1, topInset, this));
101+
selectionRangeListener = new SelectionRangeChangeListener<>(this);
102+
selections.addListener(new SelectionsSetListener<>(leftInset, selectionRangeListener, topInset, this));
100103

101-
ChangeListener<Integer> requestLayout2 = new CaretPositionChangeListener<>(this);
102-
carets.addListener(new CaretsChangeListener<>(leftInset, requestLayout2, topInset, this));
104+
caretPositionListener = new CaretPositionChangeListener<>(this);
105+
carets.addListener(new CaretsChangeListener<>(leftInset, caretPositionListener, topInset, this));
103106

104107
// XXX: see the note at highlightTextFill
105108
// highlightTextFill.addListener(new ChangeListener<Paint>() {
@@ -182,6 +185,20 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
182185
);
183186
}
184187

188+
void dispose() {
189+
for (SelectionPath p : selections.values()) {
190+
p.rangeProperty().removeListener(selectionRangeListener);
191+
p.layoutXProperty().unbind();
192+
p.layoutYProperty().unbind();
193+
}
194+
195+
for (CaretNode caret : carets) {
196+
caret.columnPositionProperty().removeListener(caretPositionListener);
197+
caret.layoutXProperty().unbind();
198+
caret.layoutYProperty().unbind();
199+
}
200+
}
201+
185202
public Paragraph<PS, SEG, S> getParagraph() {
186203
return paragraph;
187204
}

0 commit comments

Comments
 (0)