Vic
Vic

Reputation: 22041

JNI fatal error when calling GetStringChars

I'm trying to write a native function for

protected static native boolean _connect(String user, String password,
                                         String machine) throws Exception;

The implementation goes like this:

JNIEXPORT jboolean JNICALL Java_jniprint__1connect
(JNIEnv *env, jclass clazz, jstring _machine, jstring _user, jstring _pass)
{
cout << "Connecting !!!!!!!!" << endl;
const jchar *machine = env->GetStringChars(_machine, JNI_FALSE);    
cout << "after machine!!!!!!!!" << endl;
return JNI_FALSE;
}

It works perfectly when the _machine parameter is not null, when it is null - jvm crashes violently

 A fatal error has been detected by the Java Runtime Environment:  

  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d96019f, pid=16444, tid=10744  

 JRE version: 6.0_30-b12  
 Java VM: Java HotSpot(TM) Client VM (20.5-b03 mixed mode, sharing windows-x86 )  
 Problematic frame:  
 V  [jvm.dll+0xa019f]  

...

Whats going on?

Upvotes: 0

Views: 1745

Answers (2)

you set the string in java side equal to null. you must set that string equal to "".

Upvotes: 0

Fredrik
Fredrik

Reputation: 5839

Have you made sure you are not passing it null? EDIT: Sorry for reading sloppy, you ARE passing it null. In that case it SHOULD crash.

Also, you should call ReleaseStringchars() or future calls might crash (or give you other problems).

Edit: Running the jvm with -Xcheck:jni is often helpful when developing with JNI. Not sure it would help you here but I thought I should toss it in anyway.

Upvotes: 2

Related Questions