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.