From fa2074b0857cbebf537d7c44aceffccb05c91989 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Sun, 5 Jan 2020 10:53:26 +0200 Subject: [PATCH] Memory leak fix (#893) --- .../java/org/fxmisc/richtext/ParagraphText.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java index 755b5e364..4e2667bba 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java @@ -70,7 +70,7 @@ public ObjectProperty highlightTextFillProperty() { return highlightTextFill; } - private final Paragraph paragraph; + private Paragraph paragraph; private final CustomCssShapeHelper backgroundShapeHelper; private final CustomCssShapeHelper borderShapeHelper; @@ -221,11 +221,18 @@ public ObjectProperty highlightTextFillProperty() { } void dispose() { - // this removes listeners (in selections and carets listeners) and avoids memory leaks - selections.removeListener( selectionPathListener ); - carets.removeListener( caretNodeListener ); - selections.clear(); carets.clear(); + selections.clear(); + // The above must be before the below to prevent any memory leaks. + // Then remove listeners to also avoid memory leaks. + selections.removeListener( selectionPathListener ); + carets.removeListener( caretNodeListener ); + + getChildren().stream().filter( n -> n instanceof TextExt ).map( n -> (TextExt) n ) + .forEach( t -> JavaFXCompatibility.Text_selectionFillProperty(t).unbind() ); + + getChildren().clear(); + paragraph = null; } public Paragraph getParagraph() {