Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Call from attached thread in native code via JNI crashes. Why?

Reply
Thread Tools

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

 
 
RThaden@web.de
Guest
Posts: n/a
 
      08-02-2006
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

 
Reply With Quote
 
 
 
 
Jean-Francois Briere
Guest
Posts: n/a
 
      08-02-2006
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

 
Reply With Quote
 
 
 
 
RThaden@web.de
Guest
Posts: n/a
 
      08-02-2006
Hi Jean-Francois,

I love you, man!

Jean-Francois Briere wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
native code attached to onblur/onfocus event handler wolverine Javascript 2 11-20-2008 02:13 PM
Windows XP freezes when USB mouse/keyboard are attached but not when a serial set is attached John Dalberg Computer Support 4 06-17-2007 09:27 AM
Java Native Interface: "Translate" java call to JNI ckirchho@directmedia.de Java 13 10-09-2006 04:52 PM
How to debug through Java code and JNI with native code in c or c++ ? jin zhang Java 1 10-02-2004 08:55 AM



Advertisments