简体   繁体   English

如何跨JNI调用使用全局引用

[英]How do I use a global reference across JNI calls

I have to use assetmanager in Android to open some files. 我必须在Android中使用assetmanager来打开一些文件。 I am using the Assetmanager to open a lua file. 我正在使用Assetmanager打开一个lua文件。 To that end, I create a luastate in an init function in JNI using AAssetmanager. 为此,我使用AAssetmanager在JNI中的init函数中创建了一个luastate。 I then use this luastate to call functions in my lua file. 然后我使用这个luastate调用我的lua文件中的函数。 However, I am running an image processing application that would need to keep opening and closing this luastate for every frame which slows me down. 但是,我正在运行一个图像处理应用程序,需要为每一帧减慢打开和关闭此luastate。

I am currently doing this - 我目前正在这样做 -

JNIEXPORT jstring JNICALL
Java_com_torch_torchdemo_TorchDemo_callTorch( JNIEnv* env,
                                            jobject thiz,
                                            jobject assetManager) {
// get native asset manager

static jobject globalManager = env->NewGlobalRef(assetManager);

AAssetManager* manager = AAssetManager_fromJava(env, globalManager);
assert( NULL != manager);
lua_State *L = initstate(manager)
char file[] = "main.lua";
int ret;
long size = android_asset_get_size(file);

lua_getglobal(L,"test_func");
return nev->NewStringUTF(buffeR);
}

After this, I have no idea how to use the globalManager object in another jni function, one that I would call repeatedly. 在此之后,我不知道如何在另一个jni函数中使用globalManager对象,我将重复调用它。 The one above would be called only once. 上面的那个只会被调用一次。 I have tried directly using globalManager in another function but that gives me the error that globalManager was not defined in this scope. 我已尝试在另一个函数中直接使用globalManager,但这给了我在此范围内未定义globalManager的错误。 I cannot find any tutorials on how to use the global references either. 我找不到任何关于如何使用全局引用的教程。 The ones I found directly use the global object or class. 我发现的那些直接使用全局对象或类。 That gives me an error in my case. 这给了我一个错误。

I have tried directly using globalManager in another function but that gives me the error that globalManager was not defined in this scope. 我已尝试在另一个函数中直接使用globalManager,但这给了我在此范围内未定义globalManager的错误。

Your question is actually about the concept of global variables in C rather than global JNI references. 您的问题实际上是关于C中全局变量的概念而不是全局JNI引用。 You defined a static variable of type jobject called globalManager in the function Java_com_torch_torchdemo_TorchDemo_callTorch . 在函数Java_com_torch_torchdemo_TorchDemo_callTorch定义了一个名为globalManager jobject类型的static变量。 That means that the variable keeps its value across multiple invocations of the function, but it still isn't defined globally. 这意味着变量在函数的多次调用中保持其值,但仍未全局定义。 You can only access it from within the function. 您只能从函数中访问它。 To use it in other functions, you have to define the variable globally, like this: 要在其他函数中使用它,您必须全局定义变量,如下所示:

// define it as a global variable
static jobject globalManager;

JNIEXPORT jstring JNICALL Java_com_torch_torchdemo_TorchDemo_callTorch(JNIEnv* env, jobject thiz, jobject assetManager) {
    // get native asset manager
    globalManager = env->NewGlobalRef(assetManager);

    // ...
}

If you also want to access the variable from other sourcefiles, you have to use an extern declaration there and drop the static modifier in this file. 如果您还想从其他源文件访问该变量,则必须在那里使用extern声明并将static修饰符放在此文件中。

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

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