JNI code issue on release of jstring

Discussion in 'Software' started by dhelie, May 20, 2011.

  1. dhelie

    dhelie

    Joined:
    May 20, 2011
    Messages:
    1
    Likes Received:
    0
    Hey!
    I am trying to write a C dll program that has java native calls in it. I have an C executable loading my DLL, passing a string to the DLL, which load the JVM environment into memory, creates the jstring, passes the jstring to my java method, returns a new string and the new string is passed back to the C program. The DLL is not released(cancel) because it is called every 15 seconds.

    It works fine the first time the code is executed, but when the DLL is called a second time and the newStringUTF function is executed, I get a 0xc0000005 access violation.
    I am using the DeleteLocalRef function ... what am I missing????

    Do I need to execute NewGlobalRef function?


    ============sample code below===================
    javaString=0;
    resultsString = 0;

    javaString = env->NewStringUTF(ptr1->message_buffer);

    if (env->ExceptionOccurred() )
    {
    env->ExceptionDescribe();
    env->ExceptionClear();
    jvm->DestroyJavaVM();
    return 0;
    }
    javaString = (jstring)env->CallStaticObjectMethod(cls, mid, javaString);
    if (env->ExceptionOccurred() )
    {
    env->ExceptionDescribe();
    env->ExceptionClear();
    jvm->DestroyJavaVM();
    return 0;
    }

    len = env->GetStringLength(javaString);
    memset (ptr1->message_buffer, 0, sizeof(ptr1->message_buffer) );

    env->GetStringUTFRegion (javaString, str, len, ptr1->message_buffer );
    if (env->ExceptionOccurred() )
    {
    env->ExceptionDescribe();
    env->ExceptionClear();
    jvm->DestroyJavaVM();
    return 0;
    }
    env->ReleaseStringUTFChars( javaString, str );
    if (env->ExceptionOccurred() )
    {
    env->ExceptionDescribe();
    env->ExceptionClear();
    jvm->DestroyJavaVM();
    return 0;
    }

    /* Create a global reference */
    // retString = env->NewGlobalRef(javaString);

    /* The local reference is no longer useful */
    env->DeleteLocalRef( javaString);

    /* Is the global reference created successfully? */
    /* if (retString == NULL)
    {
    printf ("\n**** out of memory\n***");
    jvm->DestroyJavaVM();
    return 0;
    }

    */
     
    dhelie, May 20, 2011
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.