Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI Memory Loss?

Reply
Thread Tools

JNI Memory Loss?

 
 
Son KwonNam
Guest
Posts: n/a
 
      06-15-2004
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 ((E-Mail Removed))
*/

#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));
 
Reply With Quote
 
 
 
 
Son KwonNam
Guest
Posts: n/a
 
      06-15-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments