Skip to content

Commit

Permalink
Delete weak or strong global JNI refs appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
paddybyers committed Aug 22, 2013
1 parent 2d99834 commit 494c397
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions bridge/src/Conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ int Conv::UnwrapObject(JNIEnv *jniEnv, Handle<Object> val, Handle<String> key, j
jobject localRef = jniEnv->NewLocalRef(extRef);
if(localRef == 0) {
/* the Java object died */
jniEnv->DeleteGlobalRef(extRef);
jniEnv->DeleteWeakGlobalRef(extRef);
val->DeleteHiddenValue(key);
} else {
/* the java object is alive */
Expand Down Expand Up @@ -1034,17 +1034,26 @@ int Conv::BindToJavaObject(JNIEnv *jniEnv, jobject jLocal, Handle<Object> val, I
return result;
}

/* called from v8 when Weak Persistent references
/* called from v8 when V8 Weak Persistent references
* to Java objects are eligible for collection */
void Conv::releaseJavaRef(Persistent<Value> instHandle, void *jGlobalRef) {
jobject ob = (jobject)jGlobalRef;
Env *env = Env::getEnv_nocheck();
JNIEnv *jniEnv = Env::getEnv_nocheck()->getVM()->getJNIEnv();
jniEnv->SetLongField(ob, env->getConv()->instHandle, 0);
jniEnv->DeleteGlobalRef(ob);
Conv::deleteGlobalRef(jniEnv, ob);
instHandle.Dispose();
}

/* called to delete a global ref when we dont know whether it's a weak or strong ref */
void Conv::deleteGlobalRef(JNIEnv *jniEnv, jobject jGlobalRef) {
if(jniEnv->GetObjectRefType(jGlobalRef) == JNIWeakGlobalRefType) {
jniEnv->DeleteWeakGlobalRef(jGlobalRef);
} else {
jniEnv->DeleteGlobalRef(jGlobalRef);
}
}

/* called by the Java environment for objects that have been finalized */
void Conv::releaseV8Handle(JNIEnv *jniEnv, Persistent<Object> val, int type) {
HandleScope scope;
Expand All @@ -1065,7 +1074,7 @@ void Conv::releaseV8Handle(JNIEnv *jniEnv, Persistent<Object> val, int type) {
Local<Value> hiddenVal = val->GetHiddenValue(sHiddenKey);
if(!hiddenVal.IsEmpty() && !hiddenVal->IsUndefined()) {
jobject extRef = (jobject)External::Unwrap(hiddenVal);
jniEnv->DeleteGlobalRef(extRef);
Conv::deleteGlobalRef(jniEnv, extRef);
val->DeleteHiddenValue(sHiddenKey);
if(interface) {
while((interface = interface->getParent())) {
Expand Down
1 change: 1 addition & 0 deletions bridge/src/Conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Conv {

void releaseV8Handle(JNIEnv *jniEnv, v8::Persistent<v8::Object> intHandle, int type);
static void releaseJavaRef(v8::Persistent<v8::Value> instHandle, void *jGlobalRef);
static void deleteGlobalRef(JNIEnv *jniEnv, jobject jGlobalRef);
static v8::Handle<v8::String> getTypeKey(unsigned int type);
jstring getJavaClassName(JNIEnv *jniEnv, jclass class_, bool replace);
v8::Handle<v8::String> getV8ClassName(JNIEnv *jniEnv, jclass class_);
Expand Down

0 comments on commit 494c397

Please sign in to comment.