Do I need to free any memory associated with parameter jobjectArray?
I have a method with following JNI method
JNIEXPORT void JNICALL Java_com_android_Coordinates_Updates
(
JNIEnv * env,
jobject obj,
jobjectArray coordinates
)
{
int size = env->GetArrayLength(coordinates);
for(int i = 0; i < size; i ++)
{
jfloatArray row = (jfloatArray)env->GetObjectArrayElement(coordinates, i);
jfloat *elements = env->GetFloatArrayElements(row , 0);
//Do I need to release this or any other memory?
env->ReleaseFloatArrayElements( 开发者_Python百科row , (jfloat *)elements, i);
}
}
My question is do I need to release any memory in this case?
I am asking this question because I have an Android app that passes small data(touch coordinates) with a fast pace from Java to JNI. When I move two fingers over my android device in fast speed, app crashes in under 15 seconds. I tried to narrow down the problem and came to the point where if I include line given below in method above app crashes, otherwise it doesn't.
jfloat *elements = env->GetFloatArrayElements(row , 0);
Yes. You need to call ReleaseXxxxArrayElement() when you have used GetXxxxArrayElements() to undo the work.
// why did you use "i" ?
env->ReleaseFloatArrayElements( row , (jfloat *)elements, 0);
// we cleanup local ref due to looping ("size" might be large!)
env->DeleteLocalRef(row);
Do you have an array of an array ? My guess below is what application code might looks like based on what I see in your problem, please modify it repost into the question.
Float[] firstElement = new Float[2];
firstElement[0] = 1.0f; // x value
firstElement[1] = 42.0f; // y value
Object[] coordinates = new Object[1];
coordinates[0] = firstElement;
com.android.Coordinates coordObject = new com.android.Coordinates();
coordObject.Updates(coordinates);
精彩评论