Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
RThaden@web.de - 02 Aug 2006 02:06 GMT
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
Jean-Francois Briere - 02 Aug 2006 04:57 GMT
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
RThaden@web.de - 02 Aug 2006 08:06 GMT
Hi Jean-Francois,

I love you, man!

> Why not simply:
>
[quoted text clipped - 5 lines]
>     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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.