[bug] JNI Helper Env not available

[bug] JNI Helper Env not available
0.0 0



I think I found a bug with JNIHelper.

I used JNIHelper::getStaticMethodInfo() to get information of a non-existing method. This caused a “pending exception” on JNI, so when you try to call another JNI method, you get an “Excpetion pending” exception thrown on the application and it crashes.

Doing some reading, found that you can check if an exception was thrown and clear it’s state by calling ExceptionChecked() and ExceptionClear(). These calls need a pEnv pointer, which is part of the methodInfo structure.

However, (and here’s finally the bug :slight_smile: ), if there’s a problem finding the method, methodInfo.env is not set, and thus the exception can’t be cleared:

static bool getStaticMethodInfo_(cocos2d::JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
        jmethodID methodID = 0;
        JNIEnv *pEnv = 0;
        bool bRet = false;

            if (! getEnv(&pEnv))

            jclass classID = getClassID_(className, pEnv);

            methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode);
            if (! methodID)
                LOGD("Failed to find static method id of %s", methodName);  
                break;  //// <<<<<<<<< Method not found, so we exit here, with undefined methodinfo.env!!!!

            methodinfo.classID = classID;
            methodinfo.env = pEnv;        ///// <<<<<<<< .env is set here!!!!
            methodinfo.methodID = methodID;

            bRet = true;
        } while (0);

        return bRet;

So, what I think we should do is move @ methodinfo.env = pEnv @ before the @ if ( !methodID ) @ check. A similar thing needs to be done in getMethodInfo_.