From d9a397b43dd6583085646a2329d7be3a8b40a278 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sat, 13 Apr 2024 08:42:29 -0700 Subject: [PATCH] Pull in mem leak fix from fyne-io/fyne/pull/4768 --- widget/richtext.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/widget/richtext.go b/widget/richtext.go index 1937594117..78277015a7 100644 --- a/widget/richtext.go +++ b/widget/richtext.go @@ -219,6 +219,29 @@ func (t *RichText) cachedSegmentVisual(seg RichTextSegment, offset int) fyne.Can return vis } +func (t *RichText) cleanVisualCache() { + if len(t.visualCache) <= len(t.Segments) { + return + } + var deletingSegs []RichTextSegment + for seg1 := range t.visualCache { + found := false + for _, seg2 := range t.Segments { + if seg1 == seg2 { + found = true + break + } + } + if !found { + // cached segment is not currently in t.Segments, clear it + deletingSegs = append(deletingSegs, seg1) + } + } + for _, seg := range deletingSegs { + delete(t.visualCache, seg) + } +} + // insertAt inserts the text at the specified position func (t *RichText) insertAt(pos int, runes string) { index := 0 @@ -712,6 +735,7 @@ func (r *textRenderer) Refresh() { r.Layout(r.obj.Size()) canvas.Refresh(r.obj.super()) + r.obj.cleanVisualCache() } func (r *textRenderer) layoutRow(texts []fyne.CanvasObject, align fyne.TextAlign, xPos, yPos, lineWidth float32) (float32, float32) {