Search code examples
androidjava-native-interfaceandroid-ndk

NewGlobalRef for jmethodID


I have a problem with something that otherwise seems to be an easy task. I have a native method which returns a jobjectArray of instances of a Java class (custom). What I do is get the jclass for my class I want to instantiate and return and then get the jmethodID for it's constructor.

The native method's signature is:

JNIEXPORT jobjectArray JNICALL
Java_com_mn_rootscape_utils_NativeMethods_getFilesPermissions( JNIEnv* env, jobject thizz, jobjectArray filePathsArray ) 

The namespace and constructor signature are defined as follows:

const char* kFilePermissionInfoPath = "com/mn/rootscape/utils/FilePermissionInfo";
const char* kFilePermInfoConstructorSig = "(IIIIIJJJLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V";

For the jclass and jmethodID I'm following the instructions posted here. So I'm getting their global references so I can use them later. Please note that I do not have an oninit in my lib yet, the code is just for testing for now.

The problem is that I get a segmentation fault when I try to retrieve the global reference for the jmethodID.

The code that does this is:

jclass filePermInfoCls = (*env)->FindClass(env, kFilePermissionInfoPath);
if(!filePermInfoCls)
{
    LOGE("getFilesPermissions: failed to get class reference.");
    return NULL;
}

gFilePermInfoClass = (jclass)(*env)->NewGlobalRef(env, filePermInfoCls);
LOGI("got gFilePermInfoClass");

jmethodID filePermInfoClsConstructor = (*env)->GetMethodID(env, gFilePermInfoClass, "<init>", kFilePermInfoConstructorSig1);
if(!filePermInfoClsConstructor)
{
    LOGE("getFilesPermissions: failed to get method reference.");
    return NULL;
}

gFilePermInfoClsConstructor = (jmethodID)(*env)->NewGlobalRef(env, filePermInfoClsConstructor);
LOGI("got gFilePermInfoClsConstructor");

The actual error is:

06-14 09:17:26.648: W/dalvikvm(26012): Invalid indirect reference 0x4c0bdc40 in decodeIndirectRef 06-14 09:17:26.648: E/dalvikvm(26012): VM aborting

gFilePermInfoClass and gFilePermInfoClsConstructor are global jclass and jmethodID objects, to hold the global refs.

Any help in this would be appreciated.

Thanks


Solution

  • A jmethodID is not an object. You don't need to convert it to a GlobalRef. Ditto jfieldID.