dcsimg
Problems in calling native methods in C from java code. (Object arrays)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   yogesh_jaipurkar
Posted On:   Saturday, July 21, 2001 01:34 AM

I'm trying to pass two java objects to a native function. This native function locally declares an array of one of the above objects, populates it and returns the array back to the java code. Now when the array contents are printed in the java code, a strange thing is observed. Here I am returning an array of size 2. i.e. 2 objects are present in that array. when i rpint these objects i find that the last element of the array (In my case, the second one) has overwritten itself on all the previous elements of the array. I had initialized the array as (4, 'S') and (8, 'P') but in java i found that the array elements were (8, 'P') and (8, 'P').. Pls help. Where is the problem?? The code is... ========================================    More>>

I'm trying to pass two java objects to a native function. This native function locally declares an array of one of the above objects, populates it and returns the array back to the java code. Now when the array contents are printed in the java code, a strange thing is observed. Here I am returning an array of size 2. i.e. 2 objects are present in that array. when i rpint these objects i find that the last element of the array (In my case, the second one) has overwritten itself on all the previous elements of the array. I had initialized the array as (4, 'S') and (8, 'P') but in java i found that the array elements were (8, 'P') and (8, 'P').. Pls help. Where is the problem??
The code is...


========================================



java code:


class retArrStruct{

public native MyArr[]
getArrStruct(MyClass obj1, MyArr obj2);

static{

System.loadLibrary

("retArrStruct");

}

public static void main(String args[]){

retArrStruct r = new
retArrStruct();

MyClass p = new MyClass();

MyArr q[] = new MyArr[2];

MyArr s = new MyArr();


q = r.getArrStruct(p,s);


System.out.println(" Value of
array element1: " +q[0].iarr);

System.out.println(" Value of array element1: " +q[0].carr);

System.out.println(" Value of array element2: " +q[1].iarr);

System.out.println(" Value of array element2: " +q[1].carr);

}

}


====================================

C code is:


1)

#include

#include "retArrStruct.h"

#include

#include "poparr.h"


JNIEXPORT jobjectArray JNICALL Java_retArrStruct_getArrStruct
(JNIEnv *env, jobject this, jobject obj1, jobject obj2){

jclass cls, cls1;

jfieldID fid, fid1, fid2;

jobjectArray result;

t_struct struct1[2];

int value, iRet, i, deval;

char dechr;


cls = (*env)->
GetObjectClass(env, obj1);

cls1 = (*env)->
GetObjectClass(env, obj2);


fid = (*env)->GetFieldID(env, cls, "avalue","I");

fid1 = (*env)->GetFieldID(env, cls1, "iarr","I");

fid2 = (*env)->GetFieldID(env, cls1, "carr","C");


result = (*env)->
NewObjectArray(env, 2, cls1, NULL);


value = (*env)->GetIntField(env, obj1, fid);


iRet = populateArrSrt(value, &struct1);


for( i = 0; i < 2; i++){

(*env)->SetIntField(env, obj2, fid1, struct1[i].version);

(*env)->SetCharField(env, obj2, fid2, struct1[i].yes);

(*env)->SetObjectArrayElement(env, result, i, obj2);

}

}


return(result);

}


------

2)
#include

#include "poparr.h"


int populateArrSrt(int id, t_struct* struct1){

printf(" Value of id in JNI:%d
", id);


struct1[0].version = 4;

struct1[0].yes = 'S';


struct1[1].version = 8;

struct1[1].yes = 'P';


return(0);


}




Sorry for a lot of printfs in the code.
They are just for debugging purpose.

Reply @ my id mailto: yogesh_jaipurkar@infy.com



TIA

yogesh    <<Less

Re: Problems in calling native methods in C from java code. (Object arrays)

Posted By:   Alexander_Krapf  
Posted On:   Sunday, July 22, 2001 07:27 AM

Hi Yogesh,


It seems to me that you're never creating new object instances in your loop. What you're doing is setting the same instance into the array over and over, and modifying its instance data along the way. You end up with the same instance in all array slots and the instance having the instance data that was last assigned.


Check out our JunC++ion tool which allows you to write code like:



MyClass::array1D cmj_retArrStruct_getArrStruct (jcpp_localenv *env, MyClass & _this )
{
MyClass::array1D result = MyClass::array1D( 2 );

//one way if you have an initializing constructor
result[ 0 ] = MyClass( 8, 'p' );
//other way if you only have default constructor
result[ 1 ] = MyClass( _use_java_ctor );
result[ 1 ].iarr = 5;
result[ 1 ].carr = 'v';

return result;
}


I hope you will admit that this code is easier to write and easier to maintain than yours. You can find out more about JunC++ion at http://www.codemesh.com. Please call us if you have any questions.


Regards,


Alex

About | Sitemap | Contact