Safe to pass objects to C functions when working in JNI Invocation API?

Posted by bubbadoughball on Stack Overflow See other posts from Stack Overflow or by bubbadoughball
Published on 2010-04-29T06:00:56Z Indexed on 2010/04/29 6:07 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

I am coding up something using the JNI Invocation API. A C program starts up a JVM and makes calls into it. The JNIenv pointer is global to the C file. I have numerous C functions which need to perform the same operation on a given class of jobject. So I wrote helper functions which take a jobject and process it, returning the needed data (a C data type...for example, an int status value). Is it safe to write C helper functions and pass jobjects to them as arguments?

i.e. (a simple example - designed to illustrate the question):

int getStatusValue(jobject jStatus)
{
  return (*jenv)->CallIntMethod(jenv,jStatus,statusMethod);
}

int function1()
{
  int status;
  jobject aObj = (*jenv)->NewObject
    (jenv,
     aDefinedClass,
     aDefinedCtor);

  jobject j = (*jenv)->CallObjectMethod
    (jenv,
     aObj,
     aDefinedObjGetMethod)

  status = getStatusValue(j);

  (*jenv)->DeleteLocalRef(jenv,aObj);
  (*jenv)->DeleteLocalRef(jenv,j);

  return status;

} 

Thanks.

© Stack Overflow or respective owner

Related posts about jni

Related posts about invocation