Call from attached thread in native code via JNI crashes. Why?

R

RThaden

Hi,

maybe somebody can help me out with this:

I have a C++ DLL which is called from a Java VM which works fine. I can
do callbacks from C++ to Java within the thread created by the VM.
I store the reference to the VM via env->GetJavaVM( &globalVM ) from
this thread in a global variable.
From a different thread I use the following code

int CALLBACK func(LPVOID pCmd, LPVOID pParam)
{

JNIEnv* localEnv;
int rc = globalVM->AttachCurrentThread((void**) &localEnv, NULL);

rc = globalVM->GetEnv( (void**)localEnv, JNI_VERSION_1_2 );

if( rc == JNI_EDETACHED )
printf("GetEnv failed: thread not attached\n");
else
if( rc == JNI_EVERSION )
printf("GetEnv failed: wrong jni version\n");
else
if( rc == JNI_OK )
printf("GetEnv ssucceded\n");

localEnv->ExceptionClear();
...

At the last command, I get an unhandled exception in the Java VM.
However, the AttachCurrentThread and the GetEnv seem to work. At least,
they don't give negative numbers.
What am I doing wrong.

Any help appreciated,

Rainer
 
J

Jean-Francois Briere

Why not simply:

int CALLBACK func(LPVOID pCmd, LPVOID pParam)
{
JNIEnv* localEnv;
int rc = globalVM->AttachCurrentThread((void**) &localEnv, NULL);

if (rc < 0)
return -1; // or whatever

localEnv->ExceptionClear();
...

Regards
 
R

RThaden

Hi Jean-Francois,

I love you, man!

Jean-Francois Briere said:
Why not simply:

int CALLBACK func(LPVOID pCmd, LPVOID pParam)
{
JNIEnv* localEnv;
int rc = globalVM->AttachCurrentThread((void**) &localEnv, NULL);

if (rc < 0)
return -1; // or whatever

This works perfectly, although I don't know why the other version
didn't work.
I am a C++ programmer and have to use Java.
I understand that a call to GetEnv is not necessary since
AttachCurrentThread already delivered a JNIEnv pointer. But why does it
crash when both are called?
Maybe it's better not to think too much about it.

Kind regards,

Rainer
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top