JNI Memory Loss?

Discussion in 'Java' started by Son KwonNam, Jun 15, 2004.

  1. Son KwonNam

    Son KwonNam Guest

    I have got c functions which change java string to c string(char *).

    But when I use these functions many times(actually not that many, just
    about 10 times...), I got Memory violation error.

    == error message =======================================================
    An unexpected exception has been detected in native code outside the VM.
    Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x10002C46
    Function=[Unknown.]
    Library=D:\Mywork\Stem\LIB\kltjni.dll
    ========================================================================

    == the C functions I use ===============================================
    /* These functions change Java String to C string*/

    /*
    * @(#)NativeStringUtil.c 1.0 98/01/02 Deogtae Kim ()
    */

    #include <stdlib.h>
    #include <string.h>
    #include <jni.h>
    #include "NativeStringUtil.h"

    static jclass class_String;
    static jmethodID mid_getBytes, mid_getBytesEncoding;
    static jmethodID mid_newString, mid_newStringEncoding;


    char *jbyteArray2cstr( JNIEnv *env, jbyteArray javaBytes )
    {
    size_t len = (*env)->GetArrayLength(env, javaBytes);
    jbyte *nativeBytes = (*env)->GetByteArrayElements(env, javaBytes, 0);
    char *nativeStr = malloc(len+1);

    strncpy( nativeStr, nativeBytes, len );
    nativeStr[len] = '\0';
    (*env)->ReleaseByteArrayElements(
    env, javaBytes, nativeBytes, JNI_ABORT);
    return nativeStr;
    }

    jbyteArray cstr2jbyteArray( JNIEnv *env, const char *nativeStr)
    {
    jbyteArray javaBytes;
    int len = strlen( nativeStr );
    javaBytes = (*env)->NewByteArray(env, len);
    (*env)->SetByteArrayRegion(
    env, javaBytes, 0, len, (jbyte *) nativeStr );
    return javaBytes;
    }


    jbyteArray javaGetBytes( JNIEnv *env, jstring str )
    {
    if ( mid_getBytes == 0 )
    { if ( class_String == 0 )
    { jclass cls = (*env)->FindClass(env, "java/lang/String");
    if ( cls == 0 )
    return 0;
    class_String = (*env)->NewGlobalRef(env, cls);
    if ( class_String == 0 )
    return 0;
    }
    mid_getBytes = (*env)->GetMethodID(
    env, class_String, "getBytes", "()[B");
    if (mid_getBytes == 0)
    return 0;
    }

    /* str.getBytes(); */
    return (*env)->CallObjectMethod( env, str, mid_getBytes );
    }

    jbyteArray javaGetBytesEncoding( JNIEnv *env, jstring str, const char
    *encoding )
    {
    if ( mid_getBytesEncoding == 0 )
    { if ( class_String == 0 )
    { jclass cls = (*env)->FindClass(env, "java/lang/String");
    if ( cls == 0 )
    return 0;
    class_String = (*env)->NewGlobalRef(env, cls);
    if ( class_String == 0 )
    return 0;
    }
    mid_getBytesEncoding = (*env)->GetMethodID(
    env, class_String, "getBytes", "(Ljava/lang/String;)[B");
    if (mid_getBytesEncoding == 0)
    return 0;
    }

    /* str.getBytes( encoding ); */
    return (*env)->CallObjectMethod(
    env, str, mid_getBytesEncoding, (*env)->NewStringUTF(env,
    encoding));
    }


    jstring javaNewString( JNIEnv *env, jbyteArray javaBytes )
    {
    if ( mid_newString == 0 )
    { if ( class_String == 0 )
    { jclass cls = (*env)->FindClass(env, "java/lang/String");
    if ( cls == 0 )
    return 0; /* ¿À·ù */
    class_String = (*env)->NewGlobalRef(env, cls);
    if ( class_String == 0 )
    return 0; /* ¿À·ù */
    }
    mid_newString = (*env)->GetMethodID(
    env, class_String, "<init>", "([B)V");
    if ( mid_newString == 0 )
    return 0;
    }

    /* new String( javaBytes ); */
    return (*env)->NewObject(
    env, class_String, mid_newString, javaBytes );
    }


    jstring javaNewStringEncoding(
    JNIEnv *env, jbyteArray javaBytes, const char *encoding )
    {
    // int len;
    jstring str;

    if ( mid_newString == 0 )
    { if ( class_String == 0 )
    { jclass cls = (*env)->FindClass(env, "java/lang/String");
    if ( cls == 0 )
    return 0;
    class_String = (*env)->NewGlobalRef(env, cls);
    if ( class_String == 0 )
    return 0;
    }
    mid_newString = (*env)->GetMethodID(
    env, class_String, "<init>", "([BLjava/lang/String;)V");
    if ( mid_newString == 0 )
    return 0;
    }

    /* new String( javaBytes, encoding ); */
    str = (*env)->NewObject(
    env, class_String, mid_newString, javaBytes,
    (*env)->NewStringUTF(env, encoding) );
    return str;
    }

    ========================================================================

    also I use these functions uin JNI C moduleslike the follwoing

    char * cString;
    cString = jbyteArray2cstr(env, javaGetBytes(env, string));

    return javaNewString(env, cstr2jbyteArray(env, keywords));
    Son KwonNam, Jun 15, 2004
    #1
    1. Advertising

  2. Son KwonNam

    Son KwonNam Guest

    Re: JNI Memory Loss? -solved

    Son KwonNam ¾´ ±Û:
    > char *jbyteArray2cstr( JNIEnv *env, jbyteArray javaBytes )
    > {
    > size_t len = (*env)->GetArrayLength(env, javaBytes);
    > jbyte *nativeBytes = (*env)->GetByteArrayElements(env, javaBytes, 0);
    > char *nativeStr = malloc(len+1);
    >
    > strncpy( nativeStr, nativeBytes, len );
    > nativeStr[len] = '\0';
    > (*env)->ReleaseByteArrayElements(
    > env, javaBytes, nativeBytes, JNI_ABORT);
    > return nativeStr;
    > }
    >


    After using this(jbyteArray2cstr) function, I didn't free the memory.
    It was the problem.
    Son KwonNam, Jun 15, 2004
    #2
    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. =?Utf-8?B?UGV0ZXJU?=
    Replies:
    3
    Views:
    390
    Yuan Ren[MSFT]
    Jan 22, 2006
  2. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    837
    Alex Hunsley
    Nov 4, 2003
  3. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    892
    Pasturel Jean-Louis
    Mar 3, 2004
  4. vasanth
    Replies:
    0
    Views:
    2,663
    vasanth
    Jan 25, 2005
  5. Ryan Bates

    IRB + C Extension = Memory Loss

    Ryan Bates, Sep 19, 2008, in forum: Ruby
    Replies:
    3
    Views:
    87
    Tim Hunter
    Sep 19, 2008
Loading...

Share This Page