2013年9月15日星期日

[Android] About the native layer NewByteArray memory allocation problem

Hello everyone , I am now experiencing a problem , the phenomenon is this:
step1:
my native layer byteArray = (* env) -> NewByteArray allocate a memory, and then move the pointer ( named nativePtr) point to java layer . There are some data that can be used .
step2:
I then just reached level pointer nativePtr java java layer again spread from native layer again nativePtr operate on the problem , this is what I ask the next question , thank you.



Also, I use the native NewByteArray () allocate memory, java will automatically recover , or do I need to reclaim their own , thank you !

------ Solution ------------------------------------ --------

GetByteArrayRegion If the last parameter (jbyte *) type
refer to the following :
char* Utility::getByteArrayValue(JNIEnv* env, jbyteArray byteArray, int* dataLength) {
    char* data = NULL;
    if (NULL != byteArray) {
        jint length = env->GetArrayLength(byteArray);

        *dataLength = length;
        if (0 < *dataLength) {
            data = new char[length];
            env->GetByteArrayRegion(byteArray, (jint)0, length, (jbyte *) data);
        }
    }
    return data;
}


PS: your code is C , so there is (* env) ->. this you do not control , the above code is in C + +
I looked at the difference between the last parameter is GetByteArrayRegion type
------ eference ------------------------ ---------------
did not understand what you mean , you do not want to return a byte array from jni ? my neighborhood directly from the source to give you an example look
private native byte[] onSaveInstanceStateNative(int handle);


static jbyteArray
onSaveInstanceState_native(JNIEnv* env, jobject clazz, jint handle)
{
    LOG_TRACE("onSaveInstanceState_native");

    jbyteArray array = NULL;

    if (handle != 0) {
        NativeCode* code = (NativeCode*)handle;
        if (code->callbacks.onSaveInstanceState != NULL) {
            size_t len = 0;
            jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len);
            if (len > 0) {
                array = env->NewByteArray(len);
                if (array != NULL) {
                    env->SetByteArrayRegion(array, 0, len, state);
                }
            }
            if (state != NULL) {
                free(state);
            }
        }
    }

    return array;
}

------ eference ------------------------------------- -
jni references java does not reclaim
------ eference ---------------------------- -----------
Oh, thank you reply , what I mean is : native New a buffer ==== >>> passed to java ==== >>> again reached native, then the operator , then it is wrong. Unclear why ?


Re : lieri111
Since it will not recover , then I passed what function to recover it, I did not find the relevant function ah ? ? ? ?
------ eference --------------------------------------- < br>
you pass java is certainly not a pointer of type jbytearray
in time to the native words are byte [] value types . native layer receives and processes on the line have any questions for me ?

------ eference ------------------------------------ ---

you pass java is certainly not a pointer of type jbytearray   
in time to the native words are byte [] value types . native layer receives and processes on the line have any questions for me ?   
 


Thank you reply , look under my code :
jbyteArray Java_com_example_test_Getdata (JNIEnv * env, jobject thiz)
{
................. / / / some additional code to get data
byteArray = (* env) -> NewByteArray (env, size);
if (byteArray == NULL)
{
return NULL;
}
(* env) -> SetByteArrayRegion (env, byteArray, 0, size, (jbyte *) data) ;
return byteArray;
}

void Java_com_example_test_Setdata (JNIEnv * env, jobject thiz, jbyteArray jbuffer, jint size)
{

jbyteArray byteArray = NULL;

jbyte data [size];
(* env) -> GetByteArrayRegion (env, jbuffer, 0, size, data); / / / <--- - the problem here , ask the next Why , thank you ! ! !
__ android_log_print (ANDROID_LOG_INFO, "test", "test -----% .2 x -% .2 x -% .2 x -%. 2x -% .2 x -% .2 x -% .2 x - ",
size,
data [0],
data [1],
data [2],
data [3],
data [4],
data [5],
data [6]);
return;
}
then call the java layer function is:

byte [] demoPtr = xxxx.Getdata ();
. . . . . . . . . . . / / Some other operations
xxxx.Setdata (demoPtr, demosize);

This native of the setData () function in the wrong, I ask next why, thank you !
------ eference --------------------------------------- < br> Oh, thank you , moved ah, so enthusiastic help , thank you ah.
there is a do not understand is :
this I used NewByteArray allocated memory , it needs its own release or java will recover , if you want to own release , which function to call me , thank you ?



------ eference ------------------------------------ ---

not control
------ eference ---------------------------- -----------
env-> DeleteLocalRef
------ eference --------------------- ------------------
(* env) -> ReleaseByteArrayElements
(* env) -> DeleteLocalRef

so that you can clean up .
------ eference ------------------------------ ---------
Hello I NewByteArray prompted to find this function ah , what header files you want to add anything ?

没有评论:

发表评论