3

I use NDK to allocate large buffer for Java:

allocNativeBuffer(JNIEnv* env, jobject cls, jlong size) {
    void* buffer = malloc(size);
    jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
    jobject globalRef = env->NewGlobalRef(directBuffer);
    return globalRef;
}

After using this buffer I deallocate it:

freeNativeBuffer(JNIEnv* env, jobject cls, jobject globalRef) {
    void *buffer = env->GetDirectBufferAddress(globalRef);
    env->DeleteGlobalRef(globalRef);
    free(buffer);
}

On Android 2.2 it works fine, but on Android 4.0.3 application crashes during DeleteGlobalRef call. What am I doing wrong?

Pavel Konovalov
  • 414
  • 4
  • 7

1 Answers1

1

The implementation of global and local references was completely overhauled in ICS, with intention to improve memory management on the Java side. Find the explanation in developers guide Read more in the developers blog.

In the nutshell, whatever you receive in a JNI function, including the globalRef parameter of freeNativeBuffer() function, are local references. You can create and keep a global reference in your C code, like this:

static jobject globalRef;

allocNativeBuffer(JNIEnv* env, jobject cls, jlong size) {
    void* buffer = malloc(size);
    jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
    globalRef = env->NewGlobalRef(directBuffer);
    return directBuffer;
}

freeNativeBuffer(JNIEnv* env, jobject cls, jobject localRef) {
    void *buffer = env->GetDirectBufferAddress(localRef);
    if (buffer == env->GetDirectBufferAddress(globalRef) {
        /* we received the object that we saved */
        env->DeleteGlobalRef(globalRef);
        free(buffer);
    }
}

PS I found the stackoverflow discussion which looks like the inspiration for your experiment. See the answer which explains that there was no need to create and delete global references in the first place.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307