在 C++ 中使用 JNI 时出现 c0000005 异常(访问冲突);

发布于 2024-11-26 09:48:28 字数 7268 浏览 4 评论 0原文

几周前我开始使用 JNI 从 C++ 调用 Java 类,今天我遇到了一个特殊的情况。我是 C++ 新手(不过熟悉 Java),所以这可能是一个 n00b 错误。我在 Java 中有一个名为 IntArray.java 的类,并在 C++ 中创建了另一个名为 IntArrayProxy 的类(分为 .h 和 .cpp 文件),以便通过 JNI 访问其方法。我还有另一个名为 IntArrayProxyTest.cpp 的源文件,用于测试 IntArrayProxy 方法。

在 IntArrayProxy 中,我使用数据成员 jobject* intArrayObject,它包含 Java 类的实例,并将其传递给 IntArrayProxy 类的每个方法。我的问题是,当我将它用作指针(jobject*)时,在插入(使用 insert)一些整数并更改其中一些整数(使用 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样本”-ljvm -o IntArrayProxyTest.exe

希望有人可以帮助我!

提前谢谢!

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;
}

I started using JNI to call Java classes from C++ some weeks ago and today I encountered a peculiar situation. I am new to C++ (familiar with Java though), so this could be a n00b error. I have this class in Java called IntArray.java and I created another class in C++ called IntArrayProxy (split in a .h and a .cpp file) in order to access its methods through JNI. I also have another source file called IntArrayProxyTest.cpp which tests the IntArrayProxy methods.

In IntArrayProxy, I use a data member jobject* intArrayObject which contains the instance of the Java class and I pass this to every method of the IntArrayProxy class. My problem is that when I use it as a pointer (jobject*), after inserting (using insert) some integers and changing some of them (using setElement), when I use the same size() method twice, the executable I create crashes giving me a c0000005 exception (Access violation).

The first thing I noticed was that there is no problem at all if I use a normal jobject (not a jobject*) and the second one was that the exception occurs when I try to call a second non-void method. insert() and setElement(int, int) are both void, so I can call them as many times as I want. I tried it with almost all the non-void methods and the same exception was thrown each time I tried to call two non-void methods.

I thought that maybe the pointer somehow changed, so I tried printing the jobject* in each method but it stayed the same. The second explanation I found in forums was that maybe the object was destroyed but I don't know how to check it and why this could happen. I spent all day searching and debugging but no luck.

I think it's irrelevant but I am using the latest (32-bit) minGW compiler on Win7(64-bit). I also use the 32-bit jvm.dll. I am using the command line to compile it (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)

Hope someone can help me!!

Thanx in advance!!

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-12-03 09:48:28

在代理构造函数中:

intArrayObject = &obj;

您正在获取堆栈上变量的地址。当构造函数退出时,地址不再有效,因此崩溃。

intArrayObject(在标头中)应该是一个 jobject,而不是一个 jobject*,并且它的各种用途应该相应地改变。

In the proxy constructor:

intArrayObject = &obj;

You're taking the address of a variable on the stack. When the constructor exits, the address is no longer valid, hence the crash.

The intArrayObject (in the header) should be a jobject, not a jobject*, and the various uses of it should be changed accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文