Skip to content

Commit

Permalink
Merge pull request #4768 from dweymouth/richtext-leak
Browse files Browse the repository at this point in the history
Fix memory leak in RichText when replacing segments
  • Loading branch information
andydotxyz committed Apr 6, 2024
2 parents dc61414 + 6c7515e commit 8c17dda
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions widget/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,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
Expand Down Expand Up @@ -726,6 +749,8 @@ 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) {
Expand Down

0 comments on commit 8c17dda

Please sign in to comment.