From 5e5d1649299212b193ce7eacc6451dcfe1e42f3d Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 13 Dec 2023 18:21:48 -0600 Subject: [PATCH] app: [macOS] move destruction to NSView.dealloc The dealloc method is where we're guaranteed the NSView is no longer used anywhere. Signed-off-by: Elias Naur --- app/os_macos.go | 7 ++++--- app/os_macos.m | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/os_macos.go b/app/os_macos.go index 1d677827c..c1305d103 100644 --- a/app/os_macos.go +++ b/app/os_macos.go @@ -869,14 +869,13 @@ func gio_onAttached(h C.uintptr_t, attached C.int) { } } -//export gio_onClose -func gio_onClose(h C.uintptr_t) { +//export gio_onDestroy +func gio_onDestroy(h C.uintptr_t) { w := windowFor(h) w.ProcessEvent(DestroyEvent{}) w.displayLink.Close() w.displayLink = nil cgo.Handle(h).Delete() - C.CFRelease(w.view) w.view = 0 } @@ -927,6 +926,8 @@ func newWindow(win *callbacks, options []Option) { return } window := C.gio_createWindow(w.view, 0, 0, 0, 0, 0, 0) + // Release our reference now that the NSWindow has it. + C.CFRelease(w.view) w.updateWindowMode() w.Configure(options) if nextTopLeft.x == 0 && nextTopLeft.y == 0 { diff --git a/app/os_macos.m b/app/os_macos.m index f6e4c5faf..0a3f62629 100644 --- a/app/os_macos.m +++ b/app/os_macos.m @@ -90,9 +90,6 @@ - (CALayer *)makeBackingLayer { } - (void)viewDidMoveToWindow { gio_onAttached(self.handle, self.window != nil ? 1 : 0); - if (self.window == nil) { - gio_onClose(self.handle); - } } - (void)mouseDown:(NSEvent *)event { handleMouse(self, event, MOUSE_DOWN, 0, 0); @@ -205,6 +202,9 @@ - (void)applicationWillUnhide:(NSNotification *)notification { - (void)applicationDidHide:(NSNotification *)notification { gio_onHide(self.handle); } +- (void)dealloc { + gio_onDestroy(self.handle); +} @end // Delegates are weakly referenced from their peers. Nothing