Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2004

Tip: Looking for answers? Try searching our database.

JNI Memory Loss?

Thread view: 
Son KwonNam - 15 Jun 2004 11:48 GMT
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 (dtkim@calab.kaist.ac.kr)
*/

#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 - 15 Jun 2004 13:44 GMT
Son KwonNam ?? ??:
> char *jbyteArray2cstr( JNIEnv *env, jbyteArray javaBytes )
> {
[quoted text clipped - 8 lines]
>     return nativeStr;
> }

After using this(jbyteArray2cstr) function, I didn't free the memory.
It was the problem.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.