Be careful in ReleaseByteaRayelements

xiaoxiao2021-04-04  289

In the official documentation of Sun, the usage of this function is as follows

..............................

(* ENV) -> ReleaseByteaRrayElements (ENV, JB,

(jbyte *) m, 0);

The Last Argument to the ReleaseByteaRrayElements Function Above Can Have The Following Values:

0: Updates to the array from within the C code are reflected in the Java language copy JNI_COMMIT:. The Java language copy is updated, but the local jbyteArray is not freed JNI_ABORT:. Changes are not copied back, but the jbyteArray is freed. ............... ..

Be careful that the last parameter, if 0 is the memory pointed to by M. If m just points to an array on a stack, this may cause a random error in memory in the Release version. You can use JNI_Commit to avoid.

In fact, the current code may be as follows.

Void KaffeJNI_ReleaseByteArrayElements (JNIEnv * env UNUSED, jbyteArray arr, jbyte * elems, jint mode) { BEGIN_EXCEPTION_HANDLING_VOID (); if (! Elems = unhand_array ((HArrayOfByte *) arr) -> body) { switch ( mode) { case JNI_COMMIT: memcpy (unhand_array ((HArrayOfByte *) arr) -> body, elems, obj_length ((HArrayOfByte *) arr) * sizeof (jbyte)); break; case 0: memcpy (unhand_array ((Harrayofbyte *) ARR) -> Body, Elems, Obj_length (HARRAYOFBYTE *) ARR) * SIZEOF (JBYTE)); KFree (Elems); Break; Case JNI_ABORT: KFREE (ELEMS); Break; } } END_EXCEPTION_HANDLING (); }

JNI_COMMIT forces the native array to be copied back to the original array in the java virtual machine. Jni_abort frees the meory allocated for the native array welying back the New Contents

转载请注明原文地址:https://www.9cbs.com/read-131849.html

New Post(0)