4

我正在尝试将静态方法从 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 还是其他方法?

4

1 回答 1

3

不需要释放 jboolean。

C++ 代码没问题。但是那个信号 7 似乎你的 Java 代码有问题。

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

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

  • 逐步尝试您正在执行的代码以检查问题所在。
于 2013-04-15T06:35:59.440 回答