c0000005 exception (Access violation) while using JNI in C++
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; //giat开发者_运维技巧i 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;
}
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.
精彩评论