jdk/src/macosx/native/sun/awt/AWTWindow.m
changeset 13009 f409e6ef0185
parent 12816 b4cabffcb0de
child 13143 31c70a66a053
--- a/jdk/src/macosx/native/sun/awt/AWTWindow.m	Fri Jun 22 16:32:39 2012 +0400
+++ b/jdk/src/macosx/native/sun/awt/AWTWindow.m	Mon Jun 25 17:27:04 2012 +0400
@@ -228,6 +228,7 @@
     }
 
     if (self.nsWindow == nil) return nil; // no hope either
+    [self.nsWindow release]; // the property retains the object already
 
     self.isEnabled = YES;
     self.javaPlatformWindow = platformWindow;
@@ -677,9 +678,9 @@
                                                   styleBits:styleBits
                                                   frameRect:frameRect
                                                 contentView:contentView];
+        // the window is released is CPlatformWindow.nativeDispose()
 
-        if (window) CFRetain(window);
-        [window release]; // GC
+        if (window) CFRetain(window.nsWindow);
     }];
 
 JNF_COCOA_EXIT(env);
@@ -1160,3 +1161,24 @@
 JNF_COCOA_EXIT(env);
 }
 
+JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeDispose
+(JNIEnv *env, jclass clazz, jlong windowPtr)
+{
+JNF_COCOA_ENTER(env);
+
+    NSWindow *nsWindow = OBJC(windowPtr);
+    [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
+        AWTWindow *window = (AWTWindow*)[nsWindow delegate];
+
+        // AWTWindow holds a reference to the NSWindow in its nsWindow
+        // property. Unsetting the delegate allows it to be deallocated
+        // which releases the reference. This, in turn, allows the window
+        // itself be deallocated.
+        [nsWindow setDelegate: nil];
+
+        [window release];
+    }];
+
+JNF_COCOA_EXIT(env);
+}
+