JNI jbyteArray

Discussion in 'Java' started by Uli Kunkel, Nov 13, 2008.

  1. Uli Kunkel

    Uli Kunkel Guest

    I call c++ dll and somethimes I get ArrayIndexOutOfBounds Exception.
    The error is in the c++ function, but I don't know where.
    Here is the relevant c++ function code:

    --------------------------------
    jbyteArray result = env->NewByteArray(DataSize);
    size_t i;

    for (i=0; i < DataSize; ++i)
    {
    jbyte n = (signed char)m_Data;
    env->SetByteArrayRegion(result, i,1, &n );
    }

    return result;
    -------------------------------------
    m_Data is unsigned char* type

    Maybe the NewByteArray couldn't allocate more space..
    Do I have to delete the jbyteArray variable somehow?
    How do I do it if I have to return it to the java?


    Any suggestions would be appretiated.
    Uli Kunkel, Nov 13, 2008
    #1
    1. Advertising

  2. Uli Kunkel

    Arne Vajhøj Guest

    Uli Kunkel wrote:
    > I call c++ dll and somethimes I get ArrayIndexOutOfBounds Exception.
    > The error is in the c++ function, but I don't know where.
    > Here is the relevant c++ function code:
    >
    > --------------------------------
    > jbyteArray result = env->NewByteArray(DataSize);
    > size_t i;
    >
    > for (i=0; i < DataSize; ++i)
    > {
    > jbyte n = (signed char)m_Data;
    > env->SetByteArrayRegion(result, i,1, &n );
    > }
    >
    > return result;
    > -------------------------------------
    > m_Data is unsigned char* type
    >
    > Maybe the NewByteArray couldn't allocate more space.


    The code looks good to me.

    My guess is that the problem related to context
    (code not shown).

    > Do I have to delete the jbyteArray variable somehow?
    > How do I do it if I have to return it to the java?


    You can simply return it to Java and in that case
    Java will make sure it gets GC'ed.

    If you need to explicit free it in C++ then use
    ReleaseByteArrayElements.

    Arne
    Arne Vajhøj, Nov 14, 2008
    #2
    1. Advertising

  3. Uli Kunkel

    Uli Kunkel Guest

    Arne Vajhøj wrote:
    > Uli Kunkel wrote:
    >> I call c++ dll and somethimes I get ArrayIndexOutOfBounds Exception.
    >> The error is in the c++ function, but I don't know where.
    >> Here is the relevant c++ function code:
    >>
    >> --------------------------------
    >> jbyteArray result = env->NewByteArray(DataSize);
    >> size_t i;
    >>
    >> for (i=0; i < DataSize; ++i)
    >> {
    >> jbyte n = (signed char)m_Data;
    >> env->SetByteArrayRegion(result, i,1, &n );
    >> }
    >>
    >> return result;
    >> -------------------------------------
    >> m_Data is unsigned char* type
    >>
    >> Maybe the NewByteArray couldn't allocate more space.

    >
    > The code looks good to me.
    >
    > My guess is that the problem related to context
    > (code not shown).
    >
    >> Do I have to delete the jbyteArray variable somehow?
    >> How do I do it if I have to return it to the java?

    >
    > You can simply return it to Java and in that case
    > Java will make sure it gets GC'ed.
    >
    > If you need to explicit free it in C++ then use
    > ReleaseByteArrayElements.
    >
    > Arne


    Anyway I changed the code to copy all the data in one pass and now I
    don't get an exception.
    Also before when I set up to caught the exception the code continued to
    work which means it wasn't a memory leak.
    Uli Kunkel, Nov 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    847
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    902
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,683
    vasanth
    Jan 25, 2005
  4. Replies:
    4
    Views:
    11,904
    Boudewijn Dijkstra
    Aug 18, 2005
  5. abhijeet.s

    Convert VARIANT to jbyteArray ?

    abhijeet.s, Feb 9, 2006, in forum: Java
    Replies:
    2
    Views:
    1,714
    Gordon Beaton
    Feb 9, 2006
Loading...

Share This Page