Jump to content
Excelsior Forums
Sign in to follow this  
powpow

JET's Bug When use JNI....

Recommended Posts

My Java Application Use JNI to visite C/C++ dll。And in C/C++ Codes I Get the point of Java Object ,then I Use C/C++ Codes Operater and Return A int[] Variable。

It Can work ok when use standard Jdk ,but use JET to Compile I Get int[] all 0x00000000 。

who can tell me why?

Share this post


Link to post
Share on other sites

Honestly speaking, I do not undertstand what you mean. Please refine your question. And post the code snippet of the problem place.

Share this post


Link to post
Share on other sites

1、define a int array in java Class。

public Class NativeTestClass{

private int[] ints;

public native void changeInts();

}

2、changeInts use JNI to change value of ints。(in C++ native Code ,Can get the point of this Object,then can change the value).

3、printf the ints ,it's right in sun standard jdk ,but all 0x00000000 in JET Compiled。

do i explain clearly?Sorry My English is so poor ,i'm chinese.

Share this post


Link to post
Share on other sites

The whole context is below:

1: On the Java Side I use a int[] array  (int[] javaSideImageData)in order to store a image pixel buffer.I use the javaSideImageData to display my image in java rich client. 

2: On the JNI C/C++ Side, there are corresponding jintarray (jintArray jbuffer) data structure refer to  int[] array (int[] javaSideImageData) on the java side.

when the operations starts (Using JET mode), the JNI C/C++ side write to the pixel buffer the  correct image data .

Code segment are :

char* outbuf = (char*) env->GetIntArrayElements(jbuffer, JNI_FALSE);

*** wirte my image data to outbuf ***

for example the outbuf array's contents are RGBA RGBA RGBA .... ,which contains 512*512 pixels.

But actually After writing the outbuf successfully,the  javaSideImageData array element are all zero.That's not I needed.

On the contrary(Using the SUN JVM), the  javaSideImageData array element are all I needed. I think this is a JET's bug!

Everyone interested on it can write a jni example like this(not very completed):

Java code:

  public class imageTest

{

  public void setImageBuffer(int[] javaSideImageData); 

}

JNI C/C++ Code:

  void (packagename)_setImageBuffer(jintarray jbuffer)

{

      char* outbuf = (char*) env->GetIntArrayElements(jbuffer, JNI_FALSE);

      for (int i =0 ;i<512*512; i++)  // the image buffer is 512 * 512

      {

            outbuf = 0x00ff0000; //(R=255 G=0 B=0 indicates it's a red image )

      }

}

then on the java side image buffer element are all zero using JET compilation.

  Could you tell me why Or anybody can solve this problem? Thank you first!

Share this post


Link to post
Share on other sites

Hi,

As I see, there are two bugs in your code snippet.

1) You are filling  char* outbuf with 0x00ff0000 values which are trunced to char, so you are filling outbuf with 0 values in fact.

2) You do not call ReleaseIntArrayElements after modification of the buffer to commit changes.

See http://java.sun.com/j2se/1.5.0/docs/guide/jni/index.html for more information about JNI.

Regards,

AlexM

Share this post


Link to post
Share on other sites

2) I have released the int array by call ReleaseIntArrayElements method,just not paste in.

It's All Test right in Sun JVM the same codes, and if use byte[] array(C/C++ side jbyteArray),Excelsior JET is right too. but not well when use int[] array.

Share this post


Link to post
Share on other sites

The outbuf has a type "char*" and in the assignment

            outbuf[i] = 0x00ff0000; //(R=255 G=0 B=0 indicates it's a red image )

the constant is truncated to char type.

As (char)0x00ff0000 == 0, your code fills outbuf with zeroes.

The correct code should look like

       int* outbuf = (int*) env->GetIntArrayElements(jbuffer, NULL);
       for (int i =0 ;i<512*512; i++)  // the image buffer is 512 * 512
       {
            outbuf[i] = 0x00ff0000; //(R=255 G=0 B=0 indicates it's a red image )
       }

Note, that this is a bug in your C code and it does not relate to Excelsior JET in any way.

Regards,

AlexM

Share this post


Link to post
Share on other sites

HI,I must explain my codes test all right on SUN JVM ,so my codes has no error.

char* outbuf = (char*) env->GetIntArrayElements(jbuffer, NULL);

this code just get the int array first memory address,it can write void* outbuf,int* outbuf too,not wrong here.If I change it to int* outbuf ,the error is also exists.

       for (int i =0 ;i<512*512; i++)  // the image buffer is 512 * 512
       {
            outbuf[i] = 0x00ff0000; //(R=255 G=0 B=0 indicates it's a red image )
       }

this code i write wrong,i just express 'do some change on outbuf'...Sorry about it!

I also send int array length to C/C++ side .So I Can do change on outbuf.

Share this post


Link to post
Share on other sites

HI,I must explain my codes test all right on SUN JVM ,so my codes has no error.

The fact that your code works with Sun JVM does not necessary mean that it has no error.

this code i write wrong,i just express 'do some change on outbuf'...Sorry about it!

So post exactly those code that you think treated incorrectly by JET, since you agree that code you post here is wrong. It seems to be that the whole sources (Java and C one) can be small enough to fit this forum thread. However test it before post (with Sun JVM and Excelsior JET)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×