diff --git a/src/macosx/native/sun/awt/CFRetainedResource.m b/src/macosx/native/sun/awt/CFRetainedResource.m index 463c6bcc49800d908fdb428c56f48708f9e86317..1371189b83f85f1510e92678a6b288ea3a4939f9 100644 --- a/src/macosx/native/sun/awt/CFRetainedResource.m +++ b/src/macosx/native/sun/awt/CFRetainedResource.m @@ -23,6 +23,7 @@ * questions. */ +#import #import #import "sun_lwawt_macosx_CFRetainedResource.h" @@ -37,7 +38,10 @@ JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CFRetainedResource_nativeCFRelease (JNIEnv *env, jclass clazz, jlong ptr, jboolean releaseOnAppKitThread) { if (releaseOnAppKitThread) { - [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){ + // Releasing resources on the main AppKit message loop only + // Releasing resources on the nested loops may cause dangling + // pointers after the nested loop is exited + [NSApp postRunnableEvent:^(){ CFRelease(jlong_to_ptr(ptr)); }]; } else { diff --git a/src/macosx/native/sun/awt/LWCToolkit.m b/src/macosx/native/sun/awt/LWCToolkit.m index 3dd759ad0e38a489cf2e1880aa9afe8fee021a98..eee81e693bd423e0fa74b6ca5b04ea6ff71a6f04 100644 --- a/src/macosx/native/sun/awt/LWCToolkit.m +++ b/src/macosx/native/sun/awt/LWCToolkit.m @@ -339,8 +339,10 @@ JNF_COCOA_ENTER(env); beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.010]]; if (processEvents) { //We do not spin a runloop here as date is nil, so does not matter which mode to use + // Processing all events excluding NSApplicationDefined which need to be processed + // on the main loop only (those events are intended for disposing resources) NSEvent *event; - if ((event = [NSApp nextEventMatchingMask:NSAnyEventMask + if ((event = [NSApp nextEventMatchingMask:(NSAnyEventMask & ~NSApplicationDefined) untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]) != nil) { diff --git a/src/macosx/native/sun/osxapp/NSApplicationAWT.h b/src/macosx/native/sun/osxapp/NSApplicationAWT.h index 9269295060861858e16d99a7985e2f2d789cf24b..99c7d63175a25b25712c5d57bcad3b0b2493b121 100644 --- a/src/macosx/native/sun/osxapp/NSApplicationAWT.h +++ b/src/macosx/native/sun/osxapp/NSApplicationAWT.h @@ -37,6 +37,7 @@ - (void) registerWithProcessManager; - (void) setDockIconWithEnv:(JNIEnv *)env; - (void) postDummyEvent; +- (void) postRunnableEvent:(void (^)())block; - (void) waitForDummyEvent; + (void) runAWTLoopWithApp:(NSApplication*)app; diff --git a/src/macosx/native/sun/osxapp/NSApplicationAWT.m b/src/macosx/native/sun/osxapp/NSApplicationAWT.m index b55f1b76c21a7fd051f249c931559158c3094a72..5df99a4b8cd6721ae3ccddb3a69ec2f3a33c2cbc 100644 --- a/src/macosx/native/sun/osxapp/NSApplicationAWT.m +++ b/src/macosx/native/sun/osxapp/NSApplicationAWT.m @@ -338,9 +338,13 @@ AWT_ASSERT_APPKIT_THREAD; - (void)sendEvent:(NSEvent *)event { - if ([event type] == NSApplicationDefined && TS_EQUAL([event timestamp], dummyEventTimestamp)) { + if ([event type] == NSApplicationDefined && TS_EQUAL([event timestamp], dummyEventTimestamp) && [event subtype] == 0) { [seenDummyEventLock lockWhenCondition:NO]; [seenDummyEventLock unlockWithCondition:YES]; + } else if ([event type] == NSApplicationDefined && [event subtype] == 777) { + void (^block)() = (void (^)()) [event data1]; + block(); + [block release]; } else if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask)) { // Cocoa won't send us key up event when releasing a key while Cmd is down, // so we have to do it ourselves. @@ -350,6 +354,33 @@ AWT_ASSERT_APPKIT_THREAD; } } +/* + * Posts the block to the AppKit event queue which will be executed + * on the main AppKit loop. + * While running nested loops this event will be ignored. + */ +- (void)postRunnableEvent:(void (^)())block +{ + void (^copy)() = [block copy]; + NSInteger encode = (NSInteger) copy; + [copy retain]; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined + location: NSMakePoint(0,0) + modifierFlags: 0 + timestamp: 0 + windowNumber: 0 + context: nil + subtype: 777 + data1: encode + data2: 0]; + + [NSApp postEvent: event atStart: NO]; + [pool drain]; +} + + + - (void)postDummyEvent { seenDummyEventLock = [[NSConditionLock alloc] initWithCondition:NO]; dummyEventTimestamp = [NSProcessInfo processInfo].systemUptime; diff --git a/src/share/classes/sun/awt/datatransfer/DataTransferer.java b/src/share/classes/sun/awt/datatransfer/DataTransferer.java index 79c2792800fb7d250c0b1dcd8e0da661258d63fc..99e684d9a7b02f62744a03e268d7fbe215081fa9 100644 --- a/src/share/classes/sun/awt/datatransfer/DataTransferer.java +++ b/src/share/classes/sun/awt/datatransfer/DataTransferer.java @@ -2905,13 +2905,13 @@ search: return comp; } - if (flavor1.isFlavorTextType()) { - return 1; - } - - if (flavor2.isFlavorTextType()) { - return -1; - } +// if (flavor1.isFlavorTextType()) { +// return 1; +// } +// +// if (flavor2.isFlavorTextType()) { +// return -1; +// } // Next, look for application/x-java-* types. Prefer unknown // MIME types because if the user provides his own data flavor,