JNI

Started by CaptainJester, July 03, 2007, 02:50:47

Previous topic - Next topic

CaptainJester

Sorry guys, this is not a specific LWJGL question, but I know you use a lot of JNI in LWJGL, so I thought you guys might be able to point me in the right direction.  I have a piece of code that is getting an exception access violation an I can't figure out why.  I am trying to get the address of a ByteBuffer and that's when it dies.
JNIEXPORT jlong JNICALL Java_BufferTest(JNIEnv *env , jclass clazz, jobject testBuffer) {
    if(testBuffer== NULL) {
        printf("Buffer is null %d", testBuffer);
        return 1;
    }
    printf("Here 1\r\n");
    void* cVertexStreamZeroData = env->GetDirectBufferAddress(testBuffer);
    printf("Here 2\r\n");
    return 0;
}

Here is the calling code.
        ByteBuffer testBuffer = ByteBuffer.allocateDirect(72).order(ByteOrder.nativeOrder());;
        testBuffer.putFloat(1.0f);
        testBuffer.putFloat(2.0f);
        testBuffer.putFloat(3.0f);
        testBuffer.putFloat(4.0f);
        testBuffer.putLong(0xffffffff);
        testBuffer.putFloat(5.0f);
        testBuffer.putFloat(6.0f);
        testBuffer.putFloat(7.0f);
        testBuffer.putFloat(8.0f);
        testBuffer.putLong(0xffffffff);
        testBuffer.putFloat(9.0f);
        testBuffer.putFloat(10.0f);
        testBuffer.putFloat(11.0f);
        testBuffer.putFloat(12.0f);
        testBuffer.putLong(0xffffffff);
        testBuffer.rewind();
        Test.BufferTest(testBuffer);


Here 1 gets printed, then the exception occurs.  Any suggestions?  I am kind of a novice with C++, so I am not sure how I can debug this.

Thanks.
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

Fool Running

I'm also not good with c/c++, but the LWJGL code that is trying to do what you do looks like this:
(*env)->GetDirectBufferAddress(env, buffer)

which looks a little different than yours... Don't know if it makes any difference  ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

CaptainJester

Quote from: Fool Running on July 03, 2007, 17:05:36
I'm also not good with c/c++, but the LWJGL code that is trying to do what you do looks like this:
(*env)->GetDirectBufferAddress(env, buffer)

which looks a little different than yours... Don't know if it makes any difference  ;D

Yeah, I noticed that, but that is the C version.  Mine is the C++ version.

Thanks.
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)

gaarazero

The only suggestion I can make is that you are putting floats and longs into a ByteBuffer.  From what I've used, I only use FloatBuffers, IntBuffers, etc and put only floats, ints, etc in them, so in the JNI I can use:

JNIEXPORT void JNICALL Java_nBufferTest(JNIEnv *env, jclass, jobject buffer) {
	float* p = (float*)env->GetDirectBufferAddress( buffer );
	...
}


I'm not sure if this is your problem.  I've gotten quite a few "access violations" so I know the pain of trying to debug them.

-gz
JNewton 0.8 - Newton Dynamics wrapper for Java!
org.lwjgl.d3d - Currently Working On

CaptainJester

Quote from: gaarazero on July 19, 2007, 02:37:03
The only suggestion I can make is that you are putting floats and longs into a ByteBuffer.  From what I've used, I only use FloatBuffers, IntBuffers, etc and put only floats, ints, etc in them, so in the JNI I can use:
Quote

I have since dropped this and started over, putting everything in with the LWJGL library.  I know that is not the problem, however, because I have already had this work successfully in with LWJGL.  It may be that there is a problem trying to use C++ instead of straight C.
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)