Passing Parameters with variable values

OK,

I'll try this again.

The function in the header file (no source)is:

NEWTON_API NewtonWorld* NewtonCreate (NewtonAllocMemory malloc, NewtonFreeMemory mfree);

In the documentation are the following statements:

Parameters

NewtonAllocMemory mallocFnt - is a pointer to the memory allocator callback function. If this parameter is NULL the standard malloc function is used.

NewtonFreeMemory mfreeFnt - is a pointer to the memory release callback function. If this parameter is NULL the standard free function is used.

I implemented it like this:

JNIEXPORT jint JNICALL Java_jnewton_JNewton_newtonCreate

(JNIEnv *env, jobject class, jobject obj1, jobject obj2){

jint result;

jobject lrObj1 = (*env)->NewLocalRef(env, obj1);

jobject lrObj2 = (*env)->NewLocalRef(env, obj2);

NewtonWorld* nw = NewtonCreate(lrObj1, lrObj2);

result = (jint)nw;

(*env)->DeleteLocalRef(*env, lrObj1);

(*env)->DeleteLocalRef(*env, lrObj2);

return(result);

}

and prototype it like this:

publicnativeint newtonCreate(Object o1, Object o2);

and call it like this:

jn.newtonWorld = jn.newtonCreate(null,null);

My rationale is the only thing in Java which can have the values of null and something is a class instance. In order to allow for passing a variety of things I used Object.

Originally, I feed the jobjects to the DLL function, thinking that the null value would be preserved. This resulted in warnings that my pointer conversion was from incompatible types. So I createdthe local references. This got rid of the compiler warnings, but now I get a runtime error:

Stack: [0x00030000,0x00070000), sp=0x0006fa10, free space=254k

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

V [jvm.dll+0x41d78]

V [jvm.dll+0x8abb4]

j jnewton.JNewton.newtonCreate(Ljava/lang/Object;Ljava/lang/Object;)I+0

j jnewton.JNewton.main([Ljava/lang/String;)V+12

This seems to indicate that there is a problem with the two LocalRef creations.

1. What should I pass as a parameter?

2. How should I handle it inside the JNI function?

Any other advice is appreciated.

Jim

[2543 byte] By [jjones3566a] at [2007-11-26 19:33:49]
# 1

> OK,

> I'll try this again.

> The function in the header file (no source)is:

<snip>

>

> My rationale is the only thing in Java which can have

> the values of null and something is a class instance.

> In order to allow for passing a variety of things I

> used Object.

You aren't passing a variety of things to the native function. You are passing very specific things: pointers to NewtonAllocMemory and NewtonMemoryFree functions.

Do you intend to create Java equivalents of th NewtonAllocMemory and NewtonMemoryFree native types? The documentation you have states that these are pointers to callback functions. Creating these callbacks across JNI is possible, but it is a lot more work than what you have here. Also, these are low level memory management tasks, which you typically don't want or need when using Java.

If you don't intend to use anything other than NULL, just write the Java method as:

public native int newtonCreate();

Then in the native code, call the native function using null arguments.

At the very least, this will let you move on to porting other aspects of the API. You can always return to this problem later and fill in the missing aspects if it seems necessary. I suspect that there is no need for these memory management function pointers in your Java API.

Jim S.

Niceguy1a at 2007-7-9 22:06:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...