Search code examples
c++java-native-interfacecocos2d-x

Is needed to release jboolean using JNI?


I 'm trying to call a Static method from C++ to Java. But I get the following error:

D/cocos2d-x debug info(29160): isInternetConnected Done, value is: 1
A/libc(29160): @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree
A/libc(29160): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)

The code is:

bool InterfaceJNI::isInternetConnected()
{
    JavaVM* jvm = JniHelper::getJavaVM();
    int status;
    JNIEnv *env;
    jmethodID mid;
    jobject jobj;

    bool isAttached = false;
    bool returnValue = false;

    CCLog("InterfaceJNI isInternetConnected");

    // Get Status
    status = jvm->GetEnv((void **) &env, JNI_VERSION_1_6);

    if(status < 0)
    {
        CCLog("isInternetConnected Status < 0 Failed to get JNI Enviroment!!!");
        status = jvm->AttachCurrentThread(&env, NULL);
        CCLog("isInternetConnected Status 2: %d", status);
        if(status < 0)
        {
            CCLog("isInternetConnected Status < 0 !!!");
            return false;
        }
        isAttached = true;
        CCLog("isInternetConnected Status isAttached: %d", isAttached);
    }

    CCLog("isInternetConnected Status: %d", status);

    CCLog("isInternetConnected Finding Class....");
    jclass mClass = env->FindClass("org/example/SocialNetwork/CCSocialNetwork");


    // Get Static bool isInternetConnection()
    CCLog("isInternetConnected Getting method....");
    mid = env->GetStaticMethodID(mClass, "isInternetConnection", "()Z");
    if (mid == 0)
    {
        CCLog("isInternetConnected FAIL GET METHOD STATIC");
        return false;
    }
    CCLog("isInternetConnected Calling method....");

    // Call Static bool isInternetConnection()
    jboolean jReturnValue = env->CallStaticBooleanMethod(mClass,mid);

    CCLog("Call done ");
    // Convert value from Java to C++
    returnValue = (bool)jReturnValue;
    CCLog("isInternetConnected Done, value is: %d", returnValue);

    if(isAttached)
        jvm->DetachCurrentThread();

    // Change for return value
    return returnValue;
}

But if I just:

// Call Static bool isInternetConnection()
    /*jboolean jReturnValue =*/ env->CallStaticBooleanMethod(mClass,mid);

    CCLog("Call done ");
    // Convert value from Java to C++
    //returnValue = (bool)jReturnValue;
    CCLog("isInternetConnected Done, value is: %d", returnValue);

    if(isAttached)
        jvm->DetachCurrentThread();

    // Change for return value
    return returnValue;

I get:

Fatal signal 7 (SIGBUS) at 0x00000000 (code=128)

So I assume that the call I making is ok.

I'm in C++, I must free the jboolean or another method?


Solution

  • Is not needed to release the jboolean.

    The C++ code is ok. But that signal 7 appear something is wrong with your Java code.

    • Check if you are using static classes with statics call.

    If this is right, check where the code you are using is. Sometimes, internal calls can send SIGBUS, so try to use code only in your class.

    • Try step by step the code you are executing to check what is wrong.