S
Son KwonNam
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 ([email protected])
*/
#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/StringV");
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));
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 ([email protected])
*/
#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/StringV");
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));