Question

I have this code which is called from onPreviewFrame() (actually, a wrapper function calls this code, but does nothing except call it), which is supposed to do some OpenCV operations on a preview frame. The problem is that It keeps throwing an OOM error after a few cycles, so i stripped the code to its minimal that still throws this error.

int ContrastMeasure(JNIEnv* env, jobject obj, jbyteArray yuv, jint pw, jint ph)
{
    if (isContrastBusy)
    {
        return 0;
    }
    isContrastBusy = true;
    jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
    delete [] _yuv;
    isContrastBusy = false;
    return 0;
}

Its been ages since I used c++, so I know my mistake could be really stupid. I tried env->DeleteLocalRef(obj);, and also 'delete _yuv;' but to no avail.

what is taking all this memory, and how do I free it?

Was it helpful?

Solution

You need to ReleaseByteArrayElements as well: (and not delete)

http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html

GetArrayElements Routines

NativeType *GetArrayElements(JNIEnv *env, ArrayType array, jboolean *isCopy);

A family of functions that returns the body of the primitive array. The result is valid until the corresponding ReleaseArrayElements() function is called. Since the returned array may be a copy of the Java array, changes made to the returned array will not necessarily be reflected in the original array until ReleaseArrayElements() is called.

See the bold part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top