Wednesday, February 19, 2014

About native layer memory allocation problem with NewByteArray


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



Also, I use the native NewByteArray () after allocating memory, java automatically reclaims, or do I need to reclaim myself, thank you!
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Did not understand what you mean, you do not want to return a byte array from jni? Me an example source code directly from the neighborhood to give you a 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;
}

Reply:
jni java references will not recycle
Reply:
Oh, thank you reply, I mean: native New to pass a buffer ==== >>> spread to native java ==== >>> again, then operate, then it is wrong. Not sure why?


Re: lieri111
Since it will not be recovered, so I passed what function to recover it, I did not find a correlation function ah? ? ? ?
Reply:
reference to the third floor sno_guo reply:
Oh, thank you reply, I mean: native New pass a buffer ==== >>> spread to native java ==== >>> again, then operate, then it is wrong. Not sure why?


Re: lieri111
Since it will not be recovered, so I passed what function to recover it, I did not find a correlation function ah? ? ? ?

You pass java is certainly not the type of pointer
jbytearrayIn time to the native word is a byte [] value types. Native layer receives and processes on the line. Has any questions for me?

Reply:
references, 4th Floor guoyoulei520 reply:
Quote: references to the third floor sno_guo reply:

Oh, thank you reply, I mean: native New to pass a buffer ==== >>> spread to native java ==== >>> again, then operate, then it is wrong. Not sure why?


Re: lieri111
Since it will not be recovered, so I passed what function to recover it, I did not find a correlation function ah? ? ? ?

You pass java is certainly not the type of pointer
jbytearrayIn time to the native word is a byte [] value types. Native layer receives and processes on the line. Has any questions for me?



Thank you reply, please see my code:
jbyteArray Java_com_example_test_Getdata (JNIEnv * env, jobject thiz)
{
Code
................. / / / some other data acquisitionbyteArray = (* 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); / / / <----- problem here, ask the next Why, thank you! ! !
__android_log_print (ANDROID_LOG_INFO, "test", "test -----% .2 x -% .2 x -% .2 x -% .2 x -% .2 x -% .2 x -% .2 x - ",
size,
data [0],
data [1],
data [2],
data [3],
data [4],
data [5],
data [6]);
return;
}
Then layer in the function call is java:

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

This () function in the native's setData a mistake, ask the next Why, thank you!
Reply:

GetByteArrayRegion last parameter, if (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 are (* env) -> this you do not control, the above code is in C + +
.I looked at the difference between the last parameter is the type GetByteArrayRegion
Reply:
Oh, thank you, moved ah, so warm and helpful, thank you ah.
There is also a do not understand is:
This I NewByteArray allocated memory, or java would require the release of their recovery, if you want to release myself, what should I call the function, thank you?



Reply:
reference to the 7th floor sno_guo reply:
Oh, thank you, moved ah, so warm and helpful, thank you ah.
There is also a do not understand is:
This I NewByteArray allocated memory, or java would require the release of their recovery, if you want to release myself, what should I call the function, thank you?

Do not control
Reply:
env-> DeleteLocalRef
Reply:
(* Env) -> ReleaseByteArrayElements
(* Env) -> DeleteLocalRef

This can be cleaned up.
Reply:
reference to the 7th floor sno_guo reply:
Oh, thank you, moved ah, so warm and helpful, thank you ah.
There is also a do not understand is:
This I NewByteArray allocated memory, or java would require the release of their recovery, if you want to release myself, what should I call the function, thank you?
Hello I can not find this function ah NewByteArray tips, how to add any header files?
Reply:
reference to the 11th floor vampire_ljw reply:
Quote: references to the 7th floor sno_guo reply:

Oh, thank you, moved ah, so warm and helpful, thank you ah.
There is also a do not understand is:
This I NewByteArray allocated memory, or java would require the release of their recovery, if you want to release myself, what should I call the function, thank you?
Hello I can not find this function ah NewByteArray tips, how to add any header files?


- Plus jni interface c or cpp file: # include

No comments:

Post a Comment