从C ++调用Java方法时,应该调用PushLocalFrame和PopLocalFrame吗? [英] Should I call PushLocalFrame and PopLocalFrame when I call Java Method From C++?

查看:479
本文介绍了从C ++调用Java方法时,应该调用PushLocalFrame和PopLocalFrame吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1: 我有一个带有静态方法的Java类:

Question 1: I have a java class with a static method:

class Test {
    static public void show (String value) {
        // dosomething with value 
    }
}

如果我想从c ++调用它:

If I want call it from c++:

jstring value = env->NewStringUTF("Hello, World");
// ExceptionCheck after NewStringUTF.
env->CallStaticVoidMethod(the_jclass, the_jmethodID, value);
// ExceptionCheck after CallStaticVoidMethod.

我应该在 NewStringUTF 之前放置 PushLocalFrame ,在 CallStaticVoidMethod 之后放置 PopLocalFrame 来释放本地引用吗?

Should I put PushLocalFrame before NewStringUTF and put PopLocalFrame after CallStaticVoidMethod to release local references?

问题2:

我看到 PopLocalFrame 的声明,它是:

jobject PopLocalFrame(JNIEnv *env, jobject result);

阅读jni文档后,我仍然不明白为什么PopLocalFrame需要结果参数返回值.您能举一个例子说明它的存在价值吗?

After reading the jni documents, I still can't understand why PopLocalFrame needs a result parameter and a return value. Could you give me a example to show it's existence value?

推荐答案

我应该在NewStringUTF之前放置PushLocalFrame并在CallStaticVoidMethod之后放置PopLocalFrame来释放本地引用吗?

Should I put PushLocalFrame before NewStringUTF and put PopLocalFrame after CallStaticVoidMethod to release local references?

在这种情况下,我看不到重点.您可以只使用DeleteLocalRef删除本地参考,而不必创建新的本地参考框架.

In this particular case I don't see the point. You could just use DeleteLocalRef to delete your local reference instead of creating a new local reference frame.

我仍然不明白为什么PopLocalFrame需要一个结果参数和一个返回值

I still can't understand why PopLocalFrame needs a result parameter and a return value

假设您使用PushLocalFrame创建了一个新的本地参考框架,并且想要删除在该框架上创建的所有本地参考除了,以便保留您要保留的一个参考(例如,您创建了您要从函数中返回的某些对象).
您可以通过将要保留的引用传递到PopLocalFrame来达到此目的,并且返回的值是先前本地参考框架(现在是当前参考框架)中对该对象的引用.

Say that you created a new local reference frame with PushLocalFrame and you want to delete all local references created on that frame except for one reference that you'd like to keep (e.g. you created some object that you want to return from your function).
You achive this by passing the reference you want to keep to PopLocalFrame, and the value you get back is a reference to that object within the previous local reference frame (which now is the current one).

这篇关于从C ++调用Java方法时,应该调用PushLocalFrame和PopLocalFrame吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆