Various runtime/JNI related fixes.
NewObject returns NULL if exception is thrown in constructor.
Allocate the peer then call constructor to ensure peer_ field is
initialized.
Change thread state for AddFinalizer and pass current thread through for
ease.
Change-Id: Ib578b6d44b08aef10fde5d8bc27cc6a2acbf6fae
diff --git a/src/thread.cc b/src/thread.cc
index 983486a..af48c35 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -295,11 +295,16 @@
jboolean thread_is_daemon = as_daemon;
ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread"));
- jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
-
- ScopedLocalRef<jobject> peer(env,
- env->NewObject(c.get(), mid, thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon));
+ ScopedLocalRef<jobject> peer(env, env->AllocObject(c.get()));
peer_ = DecodeJObject(peer.get());
+ if (peer_ == NULL) {
+ CHECK(IsExceptionPending());
+ // TODO: signal failure to caller
+ return;
+ }
+ jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
+ env->CallNonvirtualVoidMethod(peer.get(), c.get(), mid, thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon);
+ CHECK(!IsExceptionPending());
SetVmData(peer_, Thread::Current());
SirtRef<String> peer_thread_name(GetName());