excpetion while calling NewObjectArray 2 times

I am calling a C++ method from another C++ code 2 times. First time, it works ok but second time, JVM throws an exception and terminates. Any idea ? It throws an exception while calling

_env->NewObjectArray

I have posted relavant portion of my method

#

# An unexpected error has been detected by HotSpot Virtual Machine:

#

# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d739bf5, pid=3592, tid=2888

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_02-b09 mixed mode)

# Problematic frame:

# V [jvm.dll+0x89bf5]

#

# An error report file with more information is saved as hs_err_pid3592.log

#

# If you would like to submit a bug report, please visit:

#http://java.sun.com/webapps/bugreport/crash.jsp

#

jclass cls;

jmethodID mid;

jstring domainString;

jstring userLogonNameString;

jstring userCommonNameString;

jstring userDisplayNameString;

jstring userDescriptionString;

domainString = _env->NewStringUTF(domain);

userLogonNameString = _env->NewStringUTF(userLogonName);

userCommonNameString = _env->NewStringUTF(userCommonName);

userDisplayNameString = _env->NewStringUTF(userDisplayName);

userDescriptionString = _env->NewStringUTF(userDescription);

printf("After NewStringUTF\n");

cls=_env->GetObjectClass(_obj);

printf("After GetObjectClass\n");

printf("Number of Members %d\n",noOfMembers);

jobjectArray ret;

if (noOfMembers > 0)

{

ret=(jobjectArray)_env->NewObjectArray(noOfMembers,

_env->FindClass ("java/lang/String"),

_env->NewStringUTF(""));

printf("After NewObjectArray\n");

for(int i=0;i<noOfMembers;i++) {

jstring jStr = _env->NewStringUTF(memberOfArr);

_env->SetObjectArrayElement(ret,i,jStr);

_env->DeleteLocalRef(jStr);

}

mid=_env->GetMethodID(cls, "updateModel",

"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V");

if (mid == 0) {

printf("%s\n","Can't find method updateModel");

return ;

}

_env->ExceptionClear();

_env->CallVoidMethod(_obj, mid,domainString,userLogonNameString,userCommonNameString,userDisplayNameString,userDescriptionString,ret);

_env->DeleteLocalRef(domainString);

_env->DeleteLocalRef(userLogonNameString);

_env->DeleteLocalRef(userCommonNameString);

_env->DeleteLocalRef(userDisplayNameString);

_env->DeleteLocalRef(userDescriptionString);

_env->DeleteLocalRef(ret);

[2712 byte] By [ap290a] at [2007-10-2 23:06:04]
# 1

It seems that the problem is because of reusing JNIEnv*. I am storing a JNIEnv* as a class variable and using it to call Java method multiple times. During first time, it works but second time, even the jstring allocation fails. Commenting out calls to java method (ex callVoidMethod) does not result in memory allocation issue. Of course, then i can not call my java method.

Seems like after calling a java method from a native code using JNIEnv*, it is not reusable. How can i achieve my goal ?

ap290a at 2007-7-14 6:20:03 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

> Seems like after calling a java method from a native code using JNIEnv*, it is not reusable. How can i achieve my goal ?

By keeping instead another structure as a class variable: JavaVM*.

With that variable you can retrieve JNIEnv* in each C++ method you need it.

Suppose you want to use JNIEnv* in SomeCppClass::someCppMethod() method:class SomeCppClass {

private:

JavaVM * jvm;

public:

SomeCppClass();

~SomeCppClass();

void someCppMethod();

};

SomeCppClass::SomeCppClass() : jvm(NULL) {

JavaVMInitArgs vm_args;

JNIEnv * env;

//...

if (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) != 0) {

// some error handling

}

}

SomeCppClass::~SomeCppClass() {

if (jvm != NULL) {

jvm->DetachCurrentThread();

jvm->DestroyJavaVM();

}

jvm = NULL;

}

void SomeCppClass::someCppMethod() {

JNIEnv * env;

if (jvm->AttachCurrentThread((void **)&env, NULL) < 0) {

// some error handling

}

}

Regards

jfbrierea at 2007-7-14 6:20:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...