From ecc26a3394f344fc9f9b4a006803be162350b98e Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 6 Nov 2026 22:56:26 +0800 Subject: [PATCH] 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) --- internal/gui/dockflap_darwin.go | 51 ++++++++++++++++++++++- internal/gui/dockflap_leak_darwin_test.go | 35 ++++++++++++++++ internal/gui/trayshown_darwin.go | 19 +++++---- 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 internal/gui/dockflap_leak_darwin_test.go diff --git a/internal/gui/dockflap_darwin.go b/internal/gui/dockflap_darwin.go index 28fce9348..cfcebc31e 100644 --- a/internal/gui/dockflap_darwin.go +++ b/internal/gui/dockflap_darwin.go @@ -6,6 +6,7 @@ package gui #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework AppKit #import +#import #include // The Dock tile flaps as the header logo does: the icon the system draws @@ -211,10 +212,49 @@ static int mpDockPNG(const char *file, double bob, double tail, double wing, con } } +// mpDockFrameNS is a frame as an NSImage, retained. It is made on a Go +// thread, which has no autorelease pool: without one of its own, what +// AppKit autoreleases in making it (its NSCGImageSnapshotRep, holding the +// frame's 1 MB of pixels) was never let go, 29 frames a flap, for as long +// as magpie ran (#4162). +static NSImage *mpDockFrameNS(CGImageRef im) { + @autoreleasepool { + return [[NSImage alloc] initWithCGImage:im size:NSMakeSize(mpDockPx, mpDockPx)]; + } +} + +// mpDockFrameFreed makes a frame's NSImage as mpDockShow does, on the +// calling thread, lets it go, and says whether its pixels went with it: +// 1 when nothing of it is left, for tests. +static int mpDockFrameFreed(void) { + CGContextRef c = mpDockContext(); + if (!c) return -1; + CGImageRef im = CGBitmapContextCreateImage(c); + CGContextRelease(c); + if (!im) return -1; + NSImage *ns = mpDockFrameNS(im); + CGImageRelease(im); + // a weak reference to its pixels' rep; each look at it in a pool of + // its own, since objc_loadWeak autoreleases what it gives + id rep = nil; + int seen, freed; + @autoreleasepool { + objc_storeWeak(&rep, ns.representations.firstObject); + seen = objc_loadWeak(&rep) != nil; + } + [ns release]; + @autoreleasepool { + freed = objc_loadWeak(&rep) == nil; + objc_storeWeak(&rep, nil); + } + if (!seen) return -1; // nothing to watch + return freed; +} + static void mpDockShow(double bob, double tail, double wing, const double *at) { CGImageRef im = mpDockFrameImage(bob, tail, wing, at); if (!im) return; - NSImage *ns = [[NSImage alloc] initWithCGImage:im size:NSMakeSize(mpDockPx, mpDockPx)]; + NSImage *ns = mpDockFrameNS(im); CGImageRelease(im); dispatch_async(dispatch_get_main_queue(), ^{ NSDockTile *tile = NSApp.dockTile; @@ -234,6 +274,7 @@ static void mpDockStill(void) { dispatch_async(dispatch_get_main_queue(), ^{ NSDockTile *tile = NSApp.dockTile; tile.contentView = nil; + mpDockView.image = nil; // the last frame's pixels, until the next flap [tile display]; }); } @@ -368,3 +409,11 @@ func dockFramePNG(app, file string, t float64) bool { bob, tail, wing := flapAt(t) return C.mpDockPNG(cf, C.double(bob), C.double(tail), C.double(wing), at) != 0 } + +// dockFrameFreed makes a Dock frame's image on this goroutine's thread, as +// dockFlap does, lets it go, and says whether its pixels went with it; ok +// is false when there was nothing to watch. +func dockFrameFreed() (freed, ok bool) { + r := C.mpDockFrameFreed() + return r == 1, r >= 0 +} diff --git a/internal/gui/dockflap_leak_darwin_test.go b/internal/gui/dockflap_leak_darwin_test.go new file mode 100644 index 000000000..d25126cf9 --- /dev/null +++ b/internal/gui/dockflap_leak_darwin_test.go @@ -0,0 +1,35 @@ +//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") + } +} diff --git a/internal/gui/trayshown_darwin.go b/internal/gui/trayshown_darwin.go index b7210cfed..5acce977a 100644 --- a/internal/gui/trayshown_darwin.go +++ b/internal/gui/trayshown_darwin.go @@ -97,17 +97,22 @@ static NSString *mpPlaceKey(const char *name) { } // Where AppKit keeps the item named name, -1 for nowhere. +// Both run on Go threads, which have no autorelease pool of their own. static double mpTrayPlace(const char *name) { - NSUserDefaults *d = [NSUserDefaults standardUserDefaults]; - [d synchronize]; - id v = [[d persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier] ?: [[NSProcessInfo processInfo] processName]] objectForKey:mpPlaceKey(name)]; - return [v respondsToSelector:@selector(doubleValue)] ? [v doubleValue] : -1; + @autoreleasepool { + NSUserDefaults *d = [NSUserDefaults standardUserDefaults]; + [d synchronize]; + id v = [[d persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier] ?: [[NSProcessInfo processInfo] processName]] objectForKey:mpPlaceKey(name)]; + return [v respondsToSelector:@selector(doubleValue)] ? [v doubleValue] : -1; + } } static void mpSetTrayPlace(const char *name, double x) { - NSUserDefaults *d = [NSUserDefaults standardUserDefaults]; - [d setDouble:x forKey:mpPlaceKey(name)]; - [d synchronize]; + @autoreleasepool { + NSUserDefaults *d = [NSUserDefaults standardUserDefaults]; + [d setDouble:x forKey:mpPlaceKey(name)]; + [d synchronize]; + } } // Waits, off the main thread, for what is queued on it to have run.