Files
magpie/internal/gui/dockflap_leak_darwin_test.go
T
yetone ecc26a3394 gui: a Dock flap's frames are let go on macOS (#4162)
Each Dock reopen (clicking the Dock icon, or `open magpie.app` while
magpie runs) plays the Dock flap: 29 frames of 512x512 RGBA, each
handed to the Dock tile as an NSImage made on a Go thread. A Go thread
has no autorelease pool, so the NSCGImageSnapshotRep that
initWithCGImage:size: autoreleases, with its 1 MB of pixels, went to
the thread's implicit pool, which drains only when the thread exits.
Every flap kept ~30 MB for good. faithleysath's heap on 0.1.3117 had
1074 NSCGImageSnapshotRep and 16K+1008K+16K CG raster regions: the
frames of about 37 flaps.

mpDockFrameNS makes each frame's NSImage inside its own
@autoreleasepool, and mpDockStill drops the last frame from the view.
The tray's mpTrayPlace and mpSetTrayPlace run on Go threads too and
get a pool each (same class; their objects are small).

Run on mote (macOS 26, M5 Pro), a real build in a sandbox HOME
(MAGPIE_ADDR=127.0.0.1:3561), 5 reopens through `open magpie.app`,
heap/footprint/vmmap 45s after the last:
- before (12a9970d6): footprint 69 MB -> 220 MB; NSCGImageSnapshotRep
  0 -> 145 (5 x 29); CG raster data 151.3M in 447 regions, the
  reporter's 16K+1008K+16K layout. Nothing freed after 45s.
- after: footprint 75 MB -> 78 MB; NSCGImageSnapshotRep 0 (1 mid-flap,
  so the flap ran); CG raster 5.2M in 15 regions.
- TestDockFrameLeavesNothing makes a frame on a locked Go thread and
  checks its image rep is gone after release. Broken by hand (pool
  taken out of mpDockFrameNS): it fails. Restored: passes, with
  TestDockFlap* and TestTray*.
- go vet ./internal/gui, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet, go build, gofmt -l: clean.

Not tried: a Retina screen (mote's is 1x, so frames there are the same
512px), the reporter's macOS 26.6, and the hidden 1470x33 windows in
their window list.

Reported-by: cgoder
Reported-by: faithleysath
Reported-by: xgxg-mdl
Reported-by: Matrix (Discord)
2026-11-06 22:56:33 +08:00

36 lines
887 B
Go

//go:build darwin && cgo
package gui
import (
"runtime"
"testing"
)
// TestDockFrameLeavesNothing: a frame of the Dock tile's flap is made on a
// Go thread, which has no autorelease pool, and its megabyte of pixels was
// kept for good, 29 frames a flap (#4162: 1,074 of them, 1.1 GB, after a
// day). Each one made and let go here leaves nothing behind.
func TestDockFrameLeavesNothing(t *testing.T) {
done := make(chan struct{})
var freed, ok bool
go func() {
defer close(done)
// a thread of its own, as a goroutine of dockFlap's runs on
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for range 3 {
if freed, ok = dockFrameFreed(); !ok || !freed {
return
}
}
}()
<-done
if !ok {
t.Skip("no frame to watch on this system")
}
if !freed {
t.Fatal("a Dock frame's image outlived its release: its pixels are kept until the thread ends")
}
}