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

Discussion in 'Java' started by RThaden@web.de, Aug 2, 2006.

  1. Guest

    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
     
    , Aug 2, 2006
    #1
    1. Advertising

  2. 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
     
    Jean-Francois Briere, Aug 2, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Aug 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhishek Singh

    JVM crashes: Native method, JNI

    Abhishek Singh, Jul 23, 2003, in forum: Java
    Replies:
    2
    Views:
    818
    Nigel Wade
    Jul 23, 2003
  2. jin zhang
    Replies:
    1
    Views:
    1,307
    Chris Uppal
    Oct 2, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,234
    Smokey Grindel
    Dec 2, 2006
  4. bgabrhelik
    Replies:
    0
    Views:
    894
    bgabrhelik
    Sep 29, 2009
  5. wolverine
    Replies:
    2
    Views:
    174
    wolverine
    Nov 20, 2008
Loading...

Share This Page