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!!!:-(

