diff --git a/CHANGELOG.md b/CHANGELOG.md index 5adb543e0..a82daf809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -104,6 +104,7 @@ - Use the available width and final alignment for ellipsized text that wraps past `MaxLines` on Apple platforms (#965 by @J4ck1e2000). - Keep what `ui.SidebarShell`'s sidebar, footer, headers and content hold and `ui.NavHeader`'s buttons clear of the left and right safe area, as on an iPhone in landscape, without padding a page inside the shell twice, and drop the gallery's own padding for it (#835 by @houko). - Have `nsgotest` count a press of another view as a new click on the macOS and Linux targets, as headless, so a press on a page that a press just pushed or popped reaches it instead of going to the page's previous view as a double click's second (#854 by @houko). +- Free the views a view's children, a `Dynamic`, an `Each` or a text's attachments drop, those last told their layout, and a removed `List`, instead of the lists that held them keeping them past their new end, and through each the whole subtree it was in, until the list grows back that long (#1021 by @iFwu). ## [0.2.0] - 2026-11-19 diff --git a/core/commit.go b/core/commit.go index 418071d1e..d42672b95 100644 --- a/core/commit.go +++ b/core/commit.go @@ -281,6 +281,7 @@ func (t *Tree) commitRound() { } t.sendSticky() t.sizeCanvases() + clear(t.laidOut) t.laidOut = t.laidOut[:0] t.emitPictureInPictures() t.emitAccessories() @@ -1196,6 +1197,9 @@ func (t *Tree) syncChildren(p *node, want []*node) { t.items = append(t.items, driver.MountItem{Kind: driver.ItemInsert, ID: c.id, Parent: p.id, Before: before}) } p.sent.children = append(p.sent.children[:0], want...) + if len(want) < len(old) { + clear(old[len(want):]) // still in the array, which would keep the views dropped + } } // move is the insert of a view inserted before into a parent, put off diff --git a/core/core_test.go b/core/core_test.go index 052fd68cf..a18abcb13 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -799,6 +799,124 @@ func TestEachKeepsRowsOfEqualItems(t *testing.T) { } } +// A view dropped from a list of views is freed: a list cut back to fewer views keeps none of the others in its array +// past its end, where each would stay in memory with the whole subtree it was in. +func TestViewsDroppedFromListsAreFreed(t *testing.T) { + freed := func(t *testing.T, h *host, views ...weak.Pointer[node]) { + t.Helper() + goruntime.GC() + for _, v := range views { + if n := v.Value(); n != nil { + t.Errorf("%s stays in memory", n.props.TestID) + } + } + goruntime.KeepAlive(h.tree) + } + views := func(prefix string, n int, opts View) Group { + g := Group{} + for i := range n { + v := opts + v.TestID = fmt.Sprint(prefix, i) + g = append(g, v) + } + return g + } + box := func(id string) Span { + return Span{Attachment: View{TestID: id, Style: Style{Width: Pt(10), Height: Pt(6)}}} + } + + t.Run("rows an Each drops", func(t *testing.T) { + var items *Signal[[]int] + h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node { + items = NewSignalFunc(cx, []int{0, 1, 2, 3}, nil) + return View{Children: []Node{Each(items.Get, func(i int) int { return i }, + func(cx *Scope, item func() int, _ func() int) Node { return View{TestID: fmt.Sprint("row", item())} })}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("row2")), weak.Make(h.find("row3"))} + items.Set([]int{0, 1}) + h.commit() + freed(t, h, dropped...) + }) + + t.Run("views a Dynamic builds again, fewer", func(t *testing.T) { + var n *Signal[int] + h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node { + n = NewSignal(cx, 4) + return View{Children: []Node{Dynamic(func(*Scope) Node { return views("view", n.Get(), View{}) })}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("view2")), weak.Make(h.find("view3"))} + n.Set(2) + h.commit() + freed(t, h, dropped...) + }) + + t.Run("attachments an element's text is set again without", func(t *testing.T) { + var el *Element + h := newHost(t, Size{Width: 200, Height: 100}, func(cx *Scope) Node { + el = NewElement(cx, Text{Spans: []Span{{Content: "ab "}, box("box0"), box("box1")}}) + return View{Children: []Node{el}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("box0")), weak.Make(h.find("box1"))} + el.Set(Text{Spans: []Span{{Content: "ab"}}}) + h.commit() + freed(t, h, dropped...) + }) + + t.Run("attachments a text's spans drop", func(t *testing.T) { + var boxes *Signal[int] + h := newHost(t, Size{Width: 200, Height: 100}, func(cx *Scope) Node { + boxes = NewSignal(cx, 2) + return View{Children: []Node{Text{SpansFunc: func() []Span { + spans := []Span{{Content: "ab "}} + for i := range boxes.Get() { + spans = append(spans, box(fmt.Sprint("box", i))) + } + return spans + }}}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("box0")), weak.Make(h.find("box1"))} + boxes.Set(0) + h.commit() + freed(t, h, dropped...) + }) + + t.Run("views that heard their last layout", func(t *testing.T) { + var shown *Signal[bool] + h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node { + shown = NewSignal(cx, true) + return View{Children: []Node{Show{When: shown.Get, + Then: func(*Scope) Node { return views("heard", 2, View{OnLayout: func(Bounds) {}}) }, + Else: func(*Scope) Node { return views("plain", 2, View{}) }}}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("heard0")), weak.Make(h.find("heard1"))} + shown.Set(false) + h.commit() + freed(t, h, dropped...) + }) + + t.Run("lists the tree updates", func(t *testing.T) { + var shown *Signal[bool] + list := func(id string) List { + return List{TestID: id, Count: func() int { return 3 }, Style: Style{Height: Pt(90)}, + Row: func(*Scope, ListRow) Node { return View{Style: Style{Height: Pt(30)}} }} + } + h := newHost(t, Size{Width: 100, Height: 200}, func(cx *Scope) Node { + shown = NewSignal(cx, true) + return View{Children: []Node{list("kept"), Show{When: shown.Get, Then: func(*Scope) Node { return list("list") }}}} + }) + h.commit() + dropped := []weak.Pointer[node]{weak.Make(h.find("list"))} + shown.Set(false) + h.commit() + freed(t, h, dropped...) + }) +} + func TestCatchShowsFallbackAndResets(t *testing.T) { var fail *Signal[bool] var reset func() diff --git a/core/list.go b/core/list.go index deb6f451c..03cdcab3c 100644 --- a/core/list.go +++ b/core/list.go @@ -1097,6 +1097,7 @@ func (t *Tree) updateLists() bool { } changed = t.within2(l, func() bool { return t.updateList(l) }) || changed } + clear(t.lists[len(live):]) // still in the array, which would keep the lists removed t.lists = live slices.SortStableFunc(parents, func(a, b *node) int { return a.list.depth - b.list.depth }) for _, l := range parents { diff --git a/core/ops.go b/core/ops.go index aeade513e..f631cba01 100644 --- a/core/ops.go +++ b/core/ops.go @@ -255,8 +255,10 @@ func (t *Tree) setText(cx *Scope, n *node, v Text) { for _, c := range n.children { t.discard(c) } + clear(n.children) n.children = n.children[:0] t.childrenChanged(n) + clear(tn.spans) tn.spans, tn.attachments = tn.spans[:0], 0 for _, sp := range spans { style := sp.Style diff --git a/core/tree.go b/core/tree.go index da574702f..c1c800117 100644 --- a/core/tree.go +++ b/core/tree.go @@ -1024,6 +1024,7 @@ func (t *Tree) clear(region *node) { for _, c := range region.children { t.discard(c) } + clear(region.children) region.children = region.children[:0] t.childrenChanged(region) } @@ -1092,7 +1093,11 @@ func (t *Tree) discard(n *node) { // setChildren replaces a region's children with nodes already in it. func (t *Tree) setChildren(region *node, children []*node) { + old := region.children region.children = append(region.children[:0], children...) + if len(children) < len(old) { + clear(old[len(children):]) // still in the array, which would keep the views dropped + } t.childrenChanged(region) } @@ -1882,6 +1887,7 @@ func (t *Tree) mountText(cx *Scope, parent *node, v Text) { kept := make([]bool, len(olds)) moved := false // the attachments' views, or their order, changed k := 0 // the attachments so far + clear(n.text.spans) n.text.spans = n.text.spans[:0] for _, sp := range spans { if sp.Attachment == nil {