bytedeco/javacpp

JNI error on Android when passing Enum to native method

Open

#481 opened on May 27, 2021

View on GitHub
 (29 comments) (0 reactions) (0 assignees)Java (620 forks)batch import
enhancementhelp wanted

Repository metrics

Stars
 (4,279 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I have enum class that was created by Parser by using new Info("name").enumerate(). When I pass it to native method (field setter), my app crashes with following error:

A/queim.tremotes: java_vm_ext.cc:577] JNI DETECTED ERROR IN APPLICATION: attempt to access field int org.bytedeco.javacpp.tools.Generator$IntEnum.value from an object argument of type org.equeim.libtremotesf.Server$ProxyType: 0x6b
    java_vm_ext.cc:577]     in call to GetIntField
    java_vm_ext.cc:577]     from org.equeim.libtremotesf.Server org.equeim.libtremotesf.Server.proxyType(org.equeim.libtremotesf.Server$ProxyType)
A/queim.tremotes: runtime.cc:655] Runtime aborting...

That's what generated JNI method looks like:

JNIEXPORT jobject JNICALL Java_org_equeim_libtremotesf_Server_proxyType__Lorg_equeim_libtremotesf_Server_00024ProxyType_2(JNIEnv* env, jobject obj, jobject arg0) {
    libtremotesf::Server* ptr = (libtremotesf::Server*)jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));
    if (ptr == NULL) {
        env->ThrowNew(JavaCPP_getClass(env, 7), "This pointer address is NULL.");
        return 0;
    }
    jlong position = env->GetLongField(obj, JavaCPP_positionFID);
    ptr += position;
    if (arg0 == NULL) {
        env->ThrowNew(JavaCPP_getClass(env, 7), "Enum for argument 0 is NULL.");
        return 0;
    }
    libtremotesf::Server::ProxyType val0 = (libtremotesf::Server::ProxyType)env->GetIntField(arg0, JavaCPP_intValueFID);
    jobject rarg = obj;
    ptr->proxyType = val0;
    return rarg;
}

This happens on Android 11 on Pixel 4A 5G.

Contributor guide