michal_l
September 23rd, 1999, 11:35 AM
Hi.
I have a problem with java-JNI.
(I'm using jdk1.2 on solaris).
I wrote a java application that calls a native (C++) method once.
This native method executes "forever" and perform callbacks to a java
constructor,and constructs new objects. It works ok for the ~3000 first
callbackes, and
then crashes with the exception:
Exception in thread "main" java.lang.stackOverflowError
I'll give you a simple example which,
I hope, will make my point clear:
This is the main java class:
====================
public class JavaLea extends Object
{
public native void init();
static
{
System.loadLibrary("leaclient");
}
public static void main(String argv[])
{
JavaLea javaLea=new JavaLea();
javaLea.init();
}//main
}
This is the native method "init"
It constructs new objects of the type "RecordRecievedEvent".
The constructor is an empty constructor which can't do any harm.
==================================================
JNIEXPORT void JNICALL Java_JavaLea_init
(JNIEnv *jenv, jobject jobj)
{
printf("begin\n");
jclass RecordRecievedEvent = jenv->FindClass("RecordRecievedEvent");
jmethodID resultConstructor
=jenv->GetMethodID(RecordRecievedEvent,"<init>","()V");
for (int i=1;true;i++)
{
jobject recordRecievedEvent
=jenv->NewObject(RecordRecievedEvent,resultConstructor);
if (jenv->ExceptionOccurred())
{
printf("!!!NewObject Exception:");
jenv->ExceptionDescribe();
jenv->ExceptionClear();
break;
}//if
delete recordRecievedEvent;
}//for
}
!!! I added the following lines to the RecordRecievedEvent's
constructor:
========================================================
System.gc();
Runtime runtime=Runtime.getRuntime();
System.out.println("totalMem:"+runtime.totalMemory()+
" freeMem:"+runtime.freeMemory());
And the free memory just go down and down till the application crashes.
Did I forget to free some memory?
I really want to know what is the right way to construct a java object
from c++.
Thanks,
Michal.
I have a problem with java-JNI.
(I'm using jdk1.2 on solaris).
I wrote a java application that calls a native (C++) method once.
This native method executes "forever" and perform callbacks to a java
constructor,and constructs new objects. It works ok for the ~3000 first
callbackes, and
then crashes with the exception:
Exception in thread "main" java.lang.stackOverflowError
I'll give you a simple example which,
I hope, will make my point clear:
This is the main java class:
====================
public class JavaLea extends Object
{
public native void init();
static
{
System.loadLibrary("leaclient");
}
public static void main(String argv[])
{
JavaLea javaLea=new JavaLea();
javaLea.init();
}//main
}
This is the native method "init"
It constructs new objects of the type "RecordRecievedEvent".
The constructor is an empty constructor which can't do any harm.
==================================================
JNIEXPORT void JNICALL Java_JavaLea_init
(JNIEnv *jenv, jobject jobj)
{
printf("begin\n");
jclass RecordRecievedEvent = jenv->FindClass("RecordRecievedEvent");
jmethodID resultConstructor
=jenv->GetMethodID(RecordRecievedEvent,"<init>","()V");
for (int i=1;true;i++)
{
jobject recordRecievedEvent
=jenv->NewObject(RecordRecievedEvent,resultConstructor);
if (jenv->ExceptionOccurred())
{
printf("!!!NewObject Exception:");
jenv->ExceptionDescribe();
jenv->ExceptionClear();
break;
}//if
delete recordRecievedEvent;
}//for
}
!!! I added the following lines to the RecordRecievedEvent's
constructor:
========================================================
System.gc();
Runtime runtime=Runtime.getRuntime();
System.out.println("totalMem:"+runtime.totalMemory()+
" freeMem:"+runtime.freeMemory());
And the free memory just go down and down till the application crashes.
Did I forget to free some memory?
I really want to know what is the right way to construct a java object
from c++.
Thanks,
Michal.