Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to call ReleaseIntArrayElements on an array created with NewIntArray?

I have a native method that does some work on a bitmap. Inside the method I grab the image data via a method call that writes the data to a jintArray parameter that I've created with NewIntArray:

jintArray pixels = env->NewIntArray(width * height);

I don't need to return this array back to the calling Java code - it's only for processing while in this method. Do I need to call ReleaseIntArrayElements on pixels? If so, what do I pass for the elems parameter, since I don't need to copy it back to a Java array?

void (JNICALL *ReleaseIntArrayElements) (JNIEnv *env, jintArray array, jint *elems, jint mode);

like image 605
Matthew Maravillas Avatar asked May 11 '12 14:05

Matthew Maravillas


People also ask

Does JNI use reflection?

Each method that can be called via JNI has a reflection metadata object. The address of this object is used as the method's jmethodID .

How JNI works?

It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++). JNI is vendor-neutral, has support for loading code from dynamic shared libraries, and while cumbersome at times is reasonably efficient.


2 Answers

You don't need to do anything with it. It is a local reference and it will be cleaned up when your JNI method exits. As Edward Thompson hints above, ReleaseIntArrayElements() is the converse of GetIntArrayElements(). It has no other function.

like image 78
user207421 Avatar answered Oct 19 '22 19:10

user207421


You have to release only reference:

jintArray pixels = env->NewIntArray(width * height);
...
env->DeleteLocalRef(pixels)
like image 1
gerbit Avatar answered Oct 19 '22 20:10

gerbit