我几周前开始使用JNI从C ++调用Java类,今天我遇到了一个奇怪的情况。我是C ++的新手(虽然熟悉Java),所以这可能是一个n00b错误。我在Java中使用了这个名为IntArray.java的类,我在C ++中创建了另一个名为IntArrayProxy的类(拆分为.h和.cpp文件),以便通过JNI访问其方法。我还有另一个名为IntArrayProxyTest.cpp的源文件,用于测试IntArrayProxy方法。
在IntArrayProxy中,我使用包含Java类实例的数据成员jobject * intArrayObject,并将其传递给IntArrayProxy类的每个方法。我的问题是,当我使用它作为指针(jobject *),插入(使用插入)一些整数并更改其中一些(使用setElement)后,当我使用相同的size()方法两次时,我创建的可执行文件崩溃给我一个c0000005异常(访问冲突)。
我注意到的第一件事是,如果我使用普通的jobject(不是jobject *)就没有问题,第二件事是当我尝试调用第二个非void方法时发生异常。 insert()和setElement(int,int)都是void,所以我可以根据需要多次调用它们。我几乎用所有非void方法尝试了它,每次我尝试调用两个非void方法时都会抛出相同的异常。
我认为指针可能会以某种方式改变,所以我尝试在每种方法中打印jobject *但它保持不变。我在论坛中发现的第二个解释是,对象可能已被破坏但我不知道如何检查它以及为什么会发生这种情况。我整天都在寻找和调试,但没有运气。
我认为这是无关紧要的,但我在Win7(64位)上使用最新的(32位)minGW编译器。我也使用32位jvm.dll。我正在使用命令行来编译它( g ++ -I“C:\ Program Files(x86)\ Java \ jdk1.6.0_26 \ include”-I“C:\ Program Files(x86)\ Java \ jdk1 .6.0_26 \ include \ win32“IntArrayProxy.cpp IntArrayProxyTest.cpp -L”C:\ Users \ jEOPARd \ Desktop \ Creta \ JNI samples“-ljvm -o IntArrayProxyTest.exe )
希望有人可以帮助我!!
Thanx提前!!
Kostis
IntArray.java
package SortIntArray;
public class IntArray {
private int[] arrayOfInt;
private int cursor;
private static final int CAPACITY = 5;
public IntArray() {
arrayOfInt = new int[CAPACITY];
cursor = 0;
}
public void insert(int n) {
if (isFull()) {
System.out.println("Inserting in a full array!");
} else {
arrayOfInt[cursor++] = n;
}
}
public int removeLast() {
if (isEmpty()) {
System.out.println("Removing from an empty array!");
return -666;
} else {
return arrayOfInt[--cursor];
}
}
private boolean isEmpty() {
return cursor <= 0;
}
private boolean isFull() {
return cursor >= CAPACITY;
}
public String toString() {
if (isEmpty()) {
return "Empty Array";
}
String s = Integer.toString(arrayOfInt[0]);
for (int i = 1; i < cursor; i++) {
s += ", " + Integer.toString(arrayOfInt[i]);
}
return s;
}
public int size() {
return cursor;
}
public int getElement(int pos) {
return arrayOfInt[pos];
}
public void setElement(int pos, int newElement) {
arrayOfInt[pos] = newElement;
}
}
IntArrayProxy.h
#ifndef INTARRAYPROXY_H
#define INTARRAYPROXY_H
#include <jni.h>
using namespace std;
class IntArrayProxy {
JNIEnv *env;
jclass intArrayClass;
jobject *intArrayObject; //giati oxi pointer?
public:
IntArrayProxy(JNIEnv*);
void insert(int n);
int removeLast();
string toString();
int size();
int getElement(int);
void setElement(int pos, int newElement);
jobject *getIntArrayObject();
};
#endif /* INTARRAYPROXY_H */
IntArrayProxy.cpp
#include <stdio.h>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "IntArrayProxy.h"
IntArrayProxy::IntArrayProxy(JNIEnv *envir) {
env = envir;
intArrayClass = env -> FindClass("SortIntArray/IntArray");
if (intArrayClass == NULL) {
cout << "--intArrayClass = NULL\n";
exit(0);
}
jmethodID IntArrayConstructor = env->GetMethodID(intArrayClass, "<init>", "()V");
if (IntArrayConstructor == NULL) {
cout << "--IntArrayConstructor = NULL";
exit(0);
}
cout << "IntArrayProxy: Got constructor\n";
jobject obj = env -> NewObject(intArrayClass, IntArrayConstructor);
intArrayObject = &obj; // I also can't assign intArrayObject directly at the above line, I don't know why (would be glad if you could tell me)
if (*intArrayObject == NULL) {
cout << "--*intArrayObject = NULL";
exit(0);
}
cout << "IntArrayProxy: Object created\n";
}
void IntArrayProxy::insert(int n) {
jmethodID insertID = env -> GetMethodID(intArrayClass, "insert", "(I)V");
if (insertID == NULL) {
cout << "--insertID = NULL";
exit(0);
}
env -> CallVoidMethod(*intArrayObject, insertID, (jint) n);
}
int IntArrayProxy::removeLast() {
jmethodID removeLastID = env -> GetMethodID(intArrayClass, "removeLast", "()I");
if (removeLastID == NULL) {
cout << "--removeLastID = NULL";
exit(0);
}
return (int) (env -> CallIntMethod(*intArrayObject, removeLastID));
}
string IntArrayProxy::toString() {
jmethodID toStringID = env -> GetMethodID(intArrayClass, "toString", "()Ljava/lang/String;");
if (toStringID == NULL) {
cout << "--toStringID = NULL";
exit(0);
}
jstring intArrayString = (jstring) env -> CallObjectMethod(*intArrayObject, toStringID);
string s = env -> GetStringUTFChars(intArrayString, NULL);
return s;
}
int IntArrayProxy::size(){
jmethodID sizeID = env -> GetMethodID(intArrayClass, "size", "()I");
if (sizeID == NULL) {
cout << "--sizeID = NULL";
exit(0);
}
return (int) (env -> CallIntMethod(*intArrayObject, sizeID));
}
int IntArrayProxy::getElement(int pos) {
jmethodID getElementID = env -> GetMethodID(intArrayClass, "getElement", "(I)I");
if (getElementID == NULL) {
cout << "--getElementID = NULL";
exit(0);
}
return (int) env -> CallObjectMethod(*intArrayObject, getElementID, (jint) pos);
}
void IntArrayProxy::setElement(int pos, int newElement){
jmethodID setElementID = env -> GetMethodID(intArrayClass, "setElement", "(II)V");
if (setElementID == NULL) {
cout << "--setElementID = NULL";
exit(0);
}
env -> CallVoidMethod(*intArrayObject, setElementID, (jint) pos, (jint) newElement);
}
jobject *IntArrayProxy::getIntArrayObject(){
return intArrayObject;
}
IntArrayProxyTest.cpp
#include <stdio.h>
#include <jni.h>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "IntArrayProxy.h"
int main() {
cout << "--Starting..\n";
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\\Users\\jEOPARd\\Desktop\\Creta\\JNI samples\\JNI tests\\build\\classes";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
cout << "--Creating VM..\n";
JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
cout << "--VM created successfully!!\n";
delete options;
cout << "--Finding IntArray class..\n";
IntArrayProxy *intArrayProxy = new IntArrayProxy(env);
if (env->ExceptionOccurred())
env->ExceptionDescribe();
intArrayProxy -> insert(1);
intArrayProxy -> insert(10);
intArrayProxy -> insert(3);
intArrayProxy -> insert(88);
intArrayProxy -> insert(32);
intArrayProxy ->setElement(2, 5);
intArrayProxy ->setElement(3, 7);
cout << "Size: " << intArrayProxy -> size() << endl;
cout << "Size: " << intArrayProxy -> size() << endl;
cout << "--Destroying VM..\n";
jvm->DestroyJavaVM();
cout << "--Done!!!\n";
return 0;
}
答案 0 :(得分:2)
在代理构造函数中:
intArrayObject =&amp; obj;
您正在获取堆栈上变量的地址。当构造函数退出时,地址不再有效,因此崩溃。
intArrayObject(在标题中)应该是一个jobject而不是jobject *,并且应该相应地更改它的各种用途。