LWJGL Forum

Programming => General Java Game Development => Topic started by: CaptainJester on July 03, 2007, 02:50:47

Title: JNI
Post by: CaptainJester on July 03, 2007, 02:50:47
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.
Title: Re: JNI
Post by: 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
Title: Re: JNI
Post by: CaptainJester on July 03, 2007, 17:30:53
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.
Title: Re: JNI
Post by: 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:


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
Title: Re: JNI
Post by: CaptainJester on July 19, 2007, 11:58:52
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.