JNI: Help on using NewObject with constructors!

E

EagerToKnow

Hi,All:

I want to create an object in a native method. The object is an
instance of an inner class. The inner class has two constructors: one
default and one with two arguments. There is no problem if I use
NewObject with the default construtor. But if I use NewObject with the
constructor taking two int arguments. The JVM crashes.

Can anybody give me some idea what the problem is?

Thanks!

Tao

Here is the native method, the inner class and error message:

JNIEXPORT jobject JNICALL Java_Prompt_createtp(JNIEnv *env, jobject
obj)
{
jobject test_ttt= 0;
jclass cls2;
jmethodID cid;
jint a,b;
jfieldID fid;

a = 10; b = 20;

cls2 = (*env)->FindClass(env,"LPrompt$tp;");
cid = (*env)->GetMethodID(env,cls2,"<init>","(LPrompt;II)V");
test_ttt = (*env)->NewObject(env,cls2,cid,a,b);

// However, the following codes using default constructor works well
//cid = (*env)->GetMethodID(env,cls2,"<init>","(LPrompt;)V");
//test_ttt = (*env)->NewObject(env,cls2,cid);

(*env)->DeleteLocalRef(env,cls2);
return test_ttt;

}

// here is the inner class
class tp{

int a;
int b;

tp(){
a = 100;
b = 0;
}

tp(int x,int y){
a = x; b = y;
}

}

// here is the crash message
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# Internal Error (53484152454432554E54494D450E43505001A3), pid=276,
tid=640
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode)
# An error report file with more information is saved as
hs_err_pid276.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
 
C

Chris Uppal

EagerToKnow said:
I want to create an object in a native method. The object is an
instance of an inner class. The inner class has two constructors: one
default and one with two arguments. There is no problem if I use
NewObject with the default construtor. But if I use NewObject with the
constructor taking two int arguments. The JVM crashes.

I can't see anything wrong with the code you posted. I may be missing
something, but if not then that suggests that your problem may be elsewhere.
Perhaps a rogue pointer or a stack corruption that causes a later crash which,
by coincidence, happens when you call the constructor.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top