Skip to content

Commit 9a1024d

Browse files
prsadhukkevinrushforth
andcommittedSep 4, 2024
8190329: [macos] Swing InterOp Platform.exit() crash
Co-authored-by: Kevin Rushforth <kcr@openjdk.org> Reviewed-by: kcr, azvegint
1 parent 5998f4b commit 9a1024d

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed
 

‎src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -117,12 +117,20 @@ + (ApplicationDelegate *)sharedDelegate {
117117
// don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari
118118
BOOL shouldInstall = NO;
119119
BOOL overrideDelegate = (getenv("AWT_OVERRIDE_NSDELEGATE") != NULL);
120+
BOOL isApplicationOwner = NO;
120121
if (NSApp != nil) {
121122
if ([NSApp isMemberOfClass:[NSApplication class]] && overrideDelegate) shouldInstall = YES;
122-
if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;
123+
if ([NSApp isKindOfClass:[NSApplicationAWT class]]) {
124+
shouldInstall = YES;
125+
isApplicationOwner = YES;
126+
}
123127
}
124128
checked = YES;
125-
if (!shouldInstall) return nil;
129+
if (!shouldInstall) {
130+
[ThreadUtilities setApplicationOwner:NO];
131+
return nil;
132+
}
133+
[ThreadUtilities setApplicationOwner:isApplicationOwner];
126134

127135
sApplicationDelegate = [[ApplicationDelegate alloc] init];
128136
return sApplicationDelegate;

‎src/java.desktop/macosx/native/libosxapp/ThreadUtilities.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -131,6 +131,7 @@ __attribute__((visibility("default")))
131131
+ (JNIEnv*)getJNIEnvUncached;
132132
+ (void)detachCurrentThread;
133133
+ (void)setAppkitThreadGroup:(jobject)group;
134+
+ (void)setApplicationOwner:(BOOL)owner;
134135

135136
+ (void)performOnMainThreadWaiting:(BOOL)wait block:(void (^)())block;
136137
+ (void)performOnMainThread:(SEL)aSelector on:(id)target withObject:(id)arg waitUntilDone:(BOOL)wait;

‎src/java.desktop/macosx/native/libosxapp/ThreadUtilities.m

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
3131

3232
// The following must be named "jvm", as there are extern references to it in AWT
3333
JavaVM *jvm = NULL;
34+
static BOOL isNSApplicationOwner = NO;
3435
static JNIEnv *appKitEnv = NULL;
3536
static jobject appkitThreadGroup = NULL;
3637
static NSString* JavaRunLoopMode = @"AWTRunLoopMode";
@@ -59,12 +60,20 @@ + (void)initialize {
5960
nil];
6061
}
6162

63+
+ (void)setApplicationOwner:(BOOL)owner {
64+
isNSApplicationOwner = owner;
65+
}
66+
6267
+ (JNIEnv*)getJNIEnv {
6368
AWT_ASSERT_APPKIT_THREAD;
64-
if (appKitEnv == NULL) {
65-
attachCurrentThread((void **)&appKitEnv);
69+
if (isNSApplicationOwner) {
70+
if (appKitEnv == NULL) {
71+
attachCurrentThread((void **)&appKitEnv);
72+
}
73+
return appKitEnv;
74+
} else {
75+
return [ThreadUtilities getJNIEnvUncached];
6676
}
67-
return appKitEnv;
6877
}
6978

7079
+ (JNIEnv*)getJNIEnvUncached {

0 commit comments

Comments
 (0)
Please sign in to comment.