-
Notifications
You must be signed in to change notification settings - Fork 148
app: [macOS] Make selecting the "Quit" menu item cause app.DestroyEvent #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…nt. Fixes #669 Signed-off-by: Lothar May <[email protected]>
gio_onDraw(self.handle); | ||
} | ||
- (void)applicationWillTerminate:(NSNotification *)notification { | ||
gio_onDestroy(self.handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is problematic if the NSWindow is later dealloc
'ed which also calls gio_onDestroy
.
According to the NSWindow.close documentation,
when the application terminates, it sends the close message to all windows in its window list
and
If the window is set to be released when closed, a release message is sent to the object after the current event is completed.
Why is NSApplication.terminate
not triggering NSWindow.release
and then a DestroyEvent?
According to https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH |
Your quoted passage just says that My question is: since index 1b94b8f93c..1ed294a420 100644
--- a/app/os_macos.m
+++ b/app/os_macos.m
@@ -19,6 +19,15 @@
@property BOOL presentWithTrans;
@end
+@interface GioWindow : NSWindow
+@end
+
+@implementation GioWindow
+- (void)dealloc {
+ NSLog(@"window dealloc");
+}
+@end
+
@implementation GioWindowDelegate
- (void)windowWillMiniaturize:(NSNotification *)notification {
NSWindow *window = (NSWindow *)[notification object];
@@ -60,6 +69,11 @@
gio_onFocus(view.handle, 0);
}
}
+- (void) windowWillClose:(NSNotification *) notification {
+ NSLog(@"CLOSE!");
+ NSWindow *window = (NSWindow *)[notification object];
+ window.contentView = nil;
+}
@end
static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFloat dy) {
@@ -377,7 +391,7 @@
NSMiniaturizableWindowMask |
NSClosableWindowMask;
- NSWindow* window = [[NSWindow alloc] initWithContentRect:rect
+ NSWindow* window = [[GioWindow alloc] initWithContentRect:rect
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO]; I get a "CLOSE", but not (NSWindow) "dealloc" when Cmd-Q'ing a program. Why? It's possible that some internal
The lifetime of the All this complexity warrants the higher-level question: what do you need the |
The section in the documentation states that dealloc does not (may not) occur on terminate. This is why dealloc is not called in this case. I see why you do not agree with macOS behaviour here, but there is not much I can do in this case. I use DestroyEvent to save global UI state, so that the application opens in the same state as it was closed. If UI state is not saved in a crash, this is not a big deal. But I don't think it is a good idea to save UI state every time it changes. And since DestroyEvent is provided, I think it should also be called when selecting the Quit menu item. |
I think the We have a more particular case: Cmd-Q invokes This doesn't happen, which leads me to conclude that
I believe persistent UI state should be saved every time it changes, in general. It's good to make programs robust to crashes in a "eat your vegetables" kind of way. However, that's not helpful to you, and I do agree a In order to generate a
|
I tried your second suggestion, by calling stop and canceling termination. However, calling stop at this point still suppresses dealloc.
If I remove the |
That leaves option 1, I'm afraid. |
What I was trying to do was to abort terminate while running stop, which could have been kind of like option 2, but it does not work. |
I can't take your patch as is. It does work in your case, but not in every other case. What about
If you don't want to checkpoint your UI state a every change, consider the more portable |
This patch fixes #669 by doing cleanup work in applicationWillTerminate as documented here: https://developer.apple.com/documentation/appkit/nsapplication/terminate(_:)