Returning a newly create object from a C Native function

Hi all,

I have a native C function thatsupposedly returns a new Object:

...

cls = (*env)->FindClass(env,"LMyClass;");

mid = (*env)->GetMethodID(env, cls,"<init>","II(V)");

return (*env)->NewObject(env, cls, mid, i, j);

}

My question is: Is this snip code correct? I mean, I've tried this and when I return from the function, a NullPointerException is thrown from the line that called the native function.

Is the reference returned by NewObject a Local Reference and as such when the funtion returns the object is garbage collected? If so a solution would be to return a GlobalReference of this new object:

...

return (*env)->NewGlobalReference(env, (*env)->NewObject(env, cls, mid, i, j) );

}

But I would like to escape this solution as I would have the task to delete all objects created in such a manner thus making little or no use at all of the JAVA's garbage collector facility.

Any ideas or sugestions are most welcome,

Ric

[1278 byte] By [Lithyum] at [2007-9-26 3:43:34]
# 1

Your signature for the constructor is wrong. It LOOKS LIKE you are tring to call a constructor paasing two ints, so try

mid = (*env)->GetMethodID(env, cls, "<init>", "(I;I)V");

If this doesn't work, then run the program javap againsta MyClass.class, and it will print for you the correct signature for your constructor.

bschauwe at 2007-6-29 12:22:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Thanks for the correction,

but that was just as example actually. Since the object's constructor has to many arguments to put there and since it was off the point I thought best to just put something simple.

I already corrected the problem. Someone here helped me this morning. The problem was way back in the code.

But thanks anyway for your input,

Ric

Lithyum at 2007-6-29 12:22:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...