IDEA-140054 A lot of NSEvents
8042752: [macosx] NSEvent instances leak throw JNI local references
Reviewed-by: serb, azvegint
diff --git a/src/macosx/native/sun/awt/AWTView.m b/src/macosx/native/sun/awt/AWTView.m
index f1a1446..c79badc 100644
--- a/src/macosx/native/sun/awt/AWTView.m
+++ b/src/macosx/native/sun/awt/AWTView.m
@@ -27,6 +27,7 @@
 
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
+#import "jni_util.h"
 
 
 #import "ThreadUtilities.h"
@@ -459,10 +460,7 @@
                                   deltaY,
                                   deltaX,
                                   [AWTToolkit scrollStateWithEvent: event]);
-    if (jEvent == nil) {
-        // Unable to create event by some reason.
-        return;
-    }
+    CHECK_NULL(jEvent);
 
     AWTWindow *awtWindow = (AWTWindow*)[event window];
 
@@ -660,7 +658,7 @@
     static JNF_CLASS_CACHE(jc_NSEvent, "sun/lwawt/macosx/NSEvent");
     static JNF_CTOR_CACHE(jctor_NSEvent, jc_NSEvent,
     "(IISLjava/lang/String;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/String;Ljava/lang/String;)V");
-    jobject jevent = JNFNewObject(env, jctor_NSEvent,
+    jobject jEvent = JNFNewObject(env, jctor_NSEvent,
                                   [event type],
                                   [event modifierFlags],
                                   [event keyCode],
@@ -672,6 +670,7 @@
                                   oldCharacters,
                                   oldCharactersIgnoringModifiers
                                   );
+    CHECK_NULL(jEvent);
 
     static JNF_CLASS_CACHE(jc_PlatformView, "sun/lwawt/macosx/CPlatformView");
     static JNF_MEMBER_CACHE(jm_deliverKeyEvent, jc_PlatformView,
@@ -679,13 +678,14 @@
 
     jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
     if (!(*env)->IsSameObject(env, jlocal, NULL)) {
-        JNFCallVoidMethod(env, jlocal, jm_deliverKeyEvent, jevent);
+        JNFCallVoidMethod(env, jlocal, jm_deliverKeyEvent, jEvent);
         (*env)->DeleteLocalRef(env, jlocal);
     }
 
     if (characters != NULL) {
         (*env)->DeleteLocalRef(env, characters);
     }
+    (*env)->DeleteLocalRef(env, jEvent);
 }
 
 -(void) deliverResize: (NSRect) rect {
diff --git a/src/macosx/native/sun/awt/CTrayIcon.m b/src/macosx/native/sun/awt/CTrayIcon.m
index e1ef711..59a767b 100644
--- a/src/macosx/native/sun/awt/CTrayIcon.m
+++ b/src/macosx/native/sun/awt/CTrayIcon.m
@@ -25,6 +25,7 @@
 
 #import <AppKit/AppKit.h>
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
+#import "jni_util.h"
 
 #import "CTrayIcon.h"
 #import "ThreadUtilities.h"
@@ -155,14 +156,12 @@
                                   deltaY,
                                   deltaX,
                                   [AWTToolkit scrollStateWithEvent: event]);
-    if (jEvent == nil) {
-        // Unable to create event by some reason.
-        return;
-    }
+    CHECK_NULL(jEvent);
 
     static JNF_CLASS_CACHE(jc_TrayIcon, "sun/lwawt/macosx/CTrayIcon");
     static JNF_MEMBER_CACHE(jm_handleMouseEvent, jc_TrayIcon, "handleMouseEvent", "(Lsun/lwawt/macosx/NSEvent;)V");
     JNFCallVoidMethod(env, peer, jm_handleMouseEvent, jEvent);
+    (*env)->DeleteLocalRef(env, jEvent);
 }
 
 @end //AWTTrayIcon