Release<PrimitiveType>ArrayElements Routines - ReleaseIntArrayElements
void
Release<PrimitiveType>ArrayElements(JNIEnv *env,
ArrayType
array,
NativeType *elems, jint mode);
A family of functions that informs the VM that the native code no longer needs access to
elems
. The elems
argument is a pointer derived from array
using the corresponding Get<
PrimitiveType>
ArrayElements() function. If necessary, this function copies back all changes made to elems
to the original array. (ReleaseIntArrayElements)
The
mode
argument provides information on how the array buffer should be released. mode
has no effect if elems
is not a copy of the elements in array
. Otherwise, mode
has the following impact, as shown in the following table:
Table 4-10 Primitive Array Release Modes (ReleaseIntArrayElements)
mode
|
actions
|
---|---|
0 |
copy back the content and free the
elems buffer |
JNI_COMMIT |
copy back the content but do not free the
elems buffer |
JNI_ABORT |
free the buffer without copying back the possible changes
|
In most cases, programmers pass “0” to the
mode
argument to ensure consistent behavior for both pinned and copied arrays. The other options give the programmer more control over memory management and should be used with extreme care. (ReleaseIntArrayElements)
The next table describes the specific routines that comprise the family of primitive array disposers. You should make the following substitutions:
- Replace Release<PrimitiveType>ArrayElements with one of the actual primitive array disposer routine names from Table 4-11.
- Replace ArrayType with the corresponding array type.
- Replace NativeType with the corresponding native type for that routine.
Table 4-11a Release<PrimitiveType>ArrayElements Family of Array Routines
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-11b Release<PrimitiveType>ArrayElements Family of Array Routines
PARAMETERS of ReleaseIntArrayElements
env
: the JNI interface pointer.array
: a Java array object.elems
: a pointer to array elements.mode
: the release mode.
Example of ReleaseIntArrayElements
JNIEXPORT jintArray JNICALL Java_SendArray_loadFile(JNIEnv *env, jobject obj, jintArray oldArray) {
const jsize length = env->GetArrayLength(oldArray);
jintArray newArray = env->NewIntArray(length);
jint *oarr = env->GetIntArrayElements(oldArray, NULL);
jint *narr = env->GetIntArrayElements(newArray, NULL);
for (int o = 0, n = length - 1; o < length; o++, n--) {
narr[n] = oarr[o];
}
env->ReleaseIntArrayElements(newArray, narr, NULL);
env->ReleaseIntArrayElements(oldArray, oarr, NULL);
return newArray;
}