Re: JNI problem
One simple mechanism is to bracket the function contents with
PushLocalFrame() and PopLocalFrame(). That will take care of all of
the local references automatically, you don't need to delete any of
them explicitly.
I tried the use of "PushLocalFrame()" and "PopLocalFrame()". And I got
the same error as if I was using "env->DeleteLocalRef(xj);" and
"env->DeleteLocalRef(cls);". I read the documentation and didn't
understand completly what it does: These functions "tracks" all the
objects created related with the "env" variable and than delete them?
My program is like that: I start the jvm and env in my main program
pass a structure with both. In eval_f I cast back this structure and
calls the java function res_fobj.
The program called the eval_f (and also res_fobj) two times. The first
one it calculates without problems, but in the second it calculates and
them return with error. It happens also a strange thing: when I run the
program with valgring ( a programm to detect memory leaks) it runs
perfectely (with many errors!!!). The programm is below (just a part of
it):
struct J1 {
JNIEnv *env;
JavaVM *jvm;};
/* Function Implementations */
Bool eval_f(Index n, Number* x, Bool new_x,
Number* obj_value, UserDataPtr user_data)
{
assert(n == 4);
J1 *jj1p = (J1*)user_data;
JNIEnv *env = jj1p->env;
jint refe;
jclass cls;
jmethodID mid;
jdoubleArray xj;
double *jxpt;
refe = env->PushLocalFrame(20);
cls = env->FindClass("res_fobj");
mid = env->GetStaticMethodID(cls, "main", "([D)D");
xj = env->NewDoubleArray(4);
jxpt = env->GetDoubleArrayElements(xj,0);
jxpt=x;
env->ReleaseDoubleArrayElements(xj, jxpt, 0);
res_obj = env->CallStaticDoubleMethod(cls, mid, xj);
cout << "resobj Calculado por java:\t" << res_obj << endl;
*obj_value = res_obj;
env->PopLocalFrame(NULL);
return TRUE;
}
Edson CV