Skip to Main Content

Java HotSpot Virtual Machine

Creating a new local ref from a weak ref because you need to call a method

843829Sep 9 2010 — edited Sep 11 2010
Hi,

I was looking at some jni code & I dont understand why you need to create a local reference from a weak one to call methods on it?

Heres the snippet:

if(NULL!=(local_ref = ((l_env.THIS))->NewLocalRef((l_env.THIS), weakRefListener)))*
*{*
l_class = ((l_env.THIS))->GetObjectClass((l_env.THIS), local_ref);*
if (l_class == ((l_env.THIS))->FindClass((l_env.THIS), "java/lang/ref/WeakReference"))*
*{*
if(NULL!=(methodID = ((l_env.THIS))->GetMethodID((l_env.THIS), l_class, (char*)"get", (char*)"()Ljava/lang/Object;")))*
*{*
strongRefListener = ((l_env.THIS))->CallObjectMethod((l_env.THIS), local_ref, methodID);*
*(*(l_env.THIS))->DeleteLocalRef((l_env.THIS), local_ref);*
*}*
*}*
else
*{*
strongRefListener = local_ref;
*}*

if (strongRefListener == NULL)
*{*
printf("failed to create strong reference");
*}*
else
*{*
*//* Call Java method *
*...... more code ..........*
*}*

Again I can't find any info as to why I need a strong reference or as to why I need to call a method "get" on it if its a weak reference. Does a weak reference get 'woken up' when invoked?


Thanks for reading this. I hope you understand it better than me.
Ryan

Edited by: mcCuppaT on Sep 9, 2010 3:35 AM

Edited by: mcCuppaT on Sep 9, 2010 3:35 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 9 2010
Added on Sep 9 2010
4 comments
128 views