是否需要使用JNI释放jboolean?

时间:2013-04-14 18:32:03

标签: c++ java-native-interface cocos2d-x

我正在尝试从C ++调用静态方法到Java。但是我收到以下错误:

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)

代码是:

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;
}

但如果我只是:

// 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;

我明白了:

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

所以我假设我打的电话没问题。

我是C ++,我必须释放jboolean或其他方法吗?

1 个答案:

答案 0 :(得分:2)

不需要释放jboolean。

C ++代码没问题。但是,这个信号7似乎出现了Java代码的错误。

  • 检查您是否使用带静态调用的静态类。

如果这是正确的,请检查您使用的代码的位置。有时,内部调用可以发送SIGBUS,因此请尝试仅在您的类中使用代码。

  • 逐步尝试执行的代码以检查错误。