From cc57413180edc4e5102f6729ccca6c4695d93e42 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 1 Nov 2013 01:03:39 +0100 Subject: Bug 882 - Crash on OSX when closing NEWT window - Check JavaVM and JNIEnv handles before usage in NewtMacWindow (Not the culprit .. but more safe) --- src/newt/native/NewtMacWindow.m | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/newt/native/NewtMacWindow.m b/src/newt/native/NewtMacWindow.m index 35d3ffbc5..686e5d2b0 100644 --- a/src/newt/native/NewtMacWindow.m +++ b/src/newt/native/NewtMacWindow.m @@ -730,9 +730,14 @@ static jint mods2JavaMods(NSUInteger mods) } int shallBeDetached = 0; JavaVM *jvmHandle = [view getJVMHandle]; - JNIEnv* env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + JNIEnv* env; + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } else { + env = NULL; + } if(NULL==env) { - DBG_PRINT("sendKeyEvent: null JNIEnv\n"); + DBG_PRINT("sendKeyEvent: JVM %p JNIEnv %p\n", jvmHandle, env); return; } @@ -781,9 +786,14 @@ static jint mods2JavaMods(NSUInteger mods) } int shallBeDetached = 0; JavaVM *jvmHandle = [view getJVMHandle]; - JNIEnv* env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + JNIEnv* env; + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } else { + env = NULL; + } if(NULL==env) { - DBG_PRINT("sendMouseEvent: null JNIEnv\n"); + DBG_PRINT("sendMouseEvent: JVM %p JNIEnv %p\n", jvmHandle, env); return; } jint javaMods[] = { 0 } ; @@ -851,9 +861,14 @@ static jint mods2JavaMods(NSUInteger mods) } int shallBeDetached = 0; JavaVM *jvmHandle = [view getJVMHandle]; - JNIEnv* env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + JNIEnv* env; + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } else { + env = NULL; + } if(NULL==env) { - DBG_PRINT("focusChanged: null JNIEnv\n"); + DBG_PRINT("focusChanged: JVM %p JNIEnv %p\n", jvmHandle, env); return; } @@ -1049,7 +1064,9 @@ static jint mods2JavaMods(NSUInteger mods) javaWindowObject = [view getJavaWindowObject]; if (javaWindowObject != NULL) { jvmHandle = [view getJVMHandle]; - env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } } } @@ -1084,9 +1101,14 @@ static jint mods2JavaMods(NSUInteger mods) } int shallBeDetached = 0; JavaVM *jvmHandle = [view getJVMHandle]; - JNIEnv* env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + JNIEnv* env; + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } else { + env = NULL; + } if(NULL==env) { - DBG_PRINT("windowDidMove: null JNIEnv\n"); + DBG_PRINT("windowDidMove: JVM %p JNIEnv %p\n", jvmHandle, env); return; } @@ -1131,9 +1153,19 @@ static jint mods2JavaMods(NSUInteger mods) } int shallBeDetached = 0; JavaVM *jvmHandle = [view getJVMHandle]; - JNIEnv* env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + JNIEnv* env = NULL; +NS_DURING + if( NULL != jvmHandle ) { + env = NewtCommon_GetJNIEnv(jvmHandle, [view getJVMVersion], 1 /* asDaemon */, &shallBeDetached); + } +NS_HANDLER + jvmHandle = NULL; + env = NULL; + [view setJVMHandle: NULL]; + DBG_PRINT("windowWillClose: JVMHandler Exception\n"); +NS_ENDHANDLER + DBG_PRINT("windowWillClose: JVM %p JNIEnv %p\n", jvmHandle, env); if(NULL==env) { - DBG_PRINT("windowWillClose: null JNIEnv\n"); return NO; } -- cgit v1.2.3