Simplest code, strangest error !!!

Hello!

I'm trying to use JNI because i want to pass a byte array from Java code to native code. But, for a simple task like this, i have a very strange problem: after a certain number of cicles, the VM crashes with an Access Violation... and if i modify a bit the code (java-side or native-side) the number of cicles changes, but the crash remain!

I put here the (essential) code because it's too simple and I just can't understand the problem!! I tried many solutions (i tried also to synchronize native and java code, else if there is only one execution-thread...), but none was succesfull...

Java side:

package prova;

publicclass ProvaNativa{

publicstaticvoid main(String[] args){

ProvaNativa prova=new ProvaNativa();

System.out.println("Loading dll...");

try{System.loadLibrary("prova_ProvaNativa");}catch(Exception e){System.out.println("Failure loading dll!!!");}

byte[] b=null;

while(true){

b=newbyte[100];

for(int i=0;i<b.length;i++) b[i]=10;

prova.copy(b);

}

}

privatenativevoid copy(byte[] bytes);

}

Native side (cpp):

#include ><jni.h>

#include <windows.h>

#include"prova_ProvaNativa.h"

byte* buf=newbyte();

JNIEXPORTvoid JNICALL Java_prova_ProvaNativa_copy(JNIEnv * env, jobject obj, jbyteArray bytes){

jbyte* array_bytes=env->GetByteArrayElements(bytes,NULL);

jint lung=env->GetArrayLength(bytes);

for(int i=0;i<lung;i++) buf[i]=array_bytes[i];

env->ReleaseByteArrayElements(bytes,array_bytes,0);

printf("Cicle finished.");

}

It crashes after some cicles...

And if I delete or add a System.out.println the number of cicles change (!!!) ....

Where is the problem?!?!?

Experts, please help me!!!:-(

[3172 byte] By [Don_Fabrizioa] at [2007-10-3 12:02:39]
# 1

You created an array of 100 bytes but initialize only 10. So on index 11 it should crash.

My suggestion - while writing JNI code get Java exception from JNI code and print it to see your problem then clear it. Because any Java exception (if you do not clear it) stops JVM and the next JNI code can throw System exception like GPF.

vitallisa at 2007-7-15 14:39:29 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
terrible distraction errors....Thank you very much, excuse me...Bye!
Don_Fabrizioa at 2007-7-15 14:39:29 > top of Java-index,Java HotSpot Virtual Machine,Specifications...