简体   繁体   English

从C ++调用非静态Java方法时出错

[英]Error while calling non static java method from c++

I have jni cpp class similar to this 我有与此类似的jni cpp类

JNIEXPORT void JNICALL Java_com_my_package_GameBridge_startGame(JNIEnv *env, 
                                                                    jobject obj)) 
{
    GameBridge_Android gameBridge;
    JavaVM* jvm;
    int status = env->GetJavaVM(&jvm);
    // global reference
    jobject globalRef = env->NewGlobalRef(obj)
    gameBridge.setCallback(globalRef);
    gameBridge.startGame();
}

I'm trying storing reference to jobject as a global reference and whenever I need I will be able to call java methods on that object. 我正在尝试将对jobject的引用存储为全局引用,只要需要,我就可以在该对象上调用java方法。

I decided to create pure c++ class - GameBridge_Android 我决定创建纯c ++类-GameBridge_Android
C++ header looks similar to this C ++标头与此类似

GameBridge_Android.h:

class GameBridge_Android : public GameBridge
{
public:
    GameBridge_Android();
    virtual void startGame();
    virtual void restartGame();
    virtual void pauseGame();
    virtual void resumeGame();
    virtual void setCallback(jobject obj);

protected:
    jobject mCallback;
};

And its realization 及其实现

GameBridge_Android.cpp:

void GameBridge_Android::restartGame(){//....}
void GameBridge_Android::resumeGame(){//....}
void GameBridge_Android::setCallback(jobject callback)
{
     mCallback = callback;
}

void GameBridge_Android::pauseGame()
{
    JavaVM* vm = JniHelper::getJavaVM();
    JNIEnv* env;
    jint rs = vm->AttachCurrentThread(&env, NULL); 

    jclass cls = env->FindClass("com/my/package/GameBridge");
    jmethodID onPause = env->GetMethodID(cls, "onPause", "()V");
    env->CallVoidMethod(mCallback, onPause);
}

And simple java GameBridge class 和简单的Java GameBridge类

public class GameBridge {
    public native void startGame();

    public void onPause() {
        Log.d("JAVA_GameBridge", "onPause called ");
    }
}

I'm getting an error 我遇到错误

 art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: native          code passing in reference to invalid global reference: 0x736c6576

 04-08 16:33:57.889  25537-29355/com.my.package A/art﹕  art/runtime/check_jni.cc:65]  in call to CallVoidMethodV

I've tried to change jobject reference to global reference but no success (probably I'm doing smth wrong here) However, when I invoke static java method, everything works perfectly ok. 我试图将jobject引用更改为全局引用,但是没有成功(可能是我在这里做错了)。但是,当我调用静态java方法时,一切正常。

PS I'm not C++ developer, so any suggestions and correction also on that part would be very appreciated. PS我不是C ++开发人员,因此对此部分的任何建议和更正将不胜感激。

First, check EVERY result from calls such as FindClass() and GetMethodID() against NULL. 首先,针对NULL检查诸如FindClass()和GetMethodID()之类的调用的每个结果。 If you get a NULL, use something like "cerr..." or "fprintf( stderr, ...)" to immediately emit a very descriptive error message. 如果得到NULL,请使用诸如“ cerr ...”或“ fprintf(stderr,...)”之类的内容立即发出非常描述性的错误消息。

Second, it's been a few years since I tried doing something very close to what you're doing, so I'm not sure, but I don't think you can reference the jobject passed to a native non-static method call outside of the context of the call it was passed to. 其次,距我尝试做非常接近您正在做的事情已经有好几年了,所以我不确定,但是我认为您不能引用传递给外部之外的本机非静态方法调用的jobject传递给它的呼叫的上下文。 You may need to use a global reference from NewGlobalRef(). 您可能需要使用NewGlobalRef()的全局引用。

JNI is very unforgiving. JNI非常宽容。

Here's another method for attaching a thread to the JVM: How to obtain JNI interface pointer (JNIEnv *) for asynchronous calls 这是将线程附加到JVM的另一种方法: 如何获取异步调用的JNI接口指针(JNIEnv *)

I seem to remember that's more like the one I had success with. 我似乎记得那更像是我成功过的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM