G
Guest
Hopefully this is an easy question.
Suppose I've got a native method that declares a bunch of memory based
on input parameters, does some work, and then returns a jintArray with
the results. ie, something like this:
JNIEXPORT jintArray JNICALL
Java_Me_doSomething (JNIEnv *jniEnv, jobject jobj, jint jx, jint jy)
{
int x = (int) jx;
int y = (int) jy;
jint mem[x * y];
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
mem[j * x + i] = getSomeValueSomehow ();
jintArray data = jniEnv->NewIntArray (x * y);
jniEnv->SetIntArrayRegion (data, 0, x * y, mem);
free (mem);
return data;
}
And, suppose that my Java program creates several Threads, all of
which call the native doSomething () method with their own params.
My question is this: is there any chance that the C++ function's
mem array can get corrupted based on the different Threads calling
doSomething? If so, then will wrapping the call to doSomething ()
inside a synchronized method prevent corruption? I'm getting
random SIGBUS and SIGSEG faults that are proving very difficult to
track down on the Solaris platform I'm using.
TIA,
Suppose I've got a native method that declares a bunch of memory based
on input parameters, does some work, and then returns a jintArray with
the results. ie, something like this:
JNIEXPORT jintArray JNICALL
Java_Me_doSomething (JNIEnv *jniEnv, jobject jobj, jint jx, jint jy)
{
int x = (int) jx;
int y = (int) jy;
jint mem[x * y];
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
mem[j * x + i] = getSomeValueSomehow ();
jintArray data = jniEnv->NewIntArray (x * y);
jniEnv->SetIntArrayRegion (data, 0, x * y, mem);
free (mem);
return data;
}
And, suppose that my Java program creates several Threads, all of
which call the native doSomething () method with their own params.
My question is this: is there any chance that the C++ function's
mem array can get corrupted based on the different Threads calling
doSomething? If so, then will wrapping the call to doSomething ()
inside a synchronized method prevent corruption? I'm getting
random SIGBUS and SIGSEG faults that are proving very difficult to
track down on the Solaris platform I'm using.
TIA,