JNI jbyteArray

U

Uli Kunkel

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.
 
A

Arne Vajhøj

Uli said:
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;


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
 
U

Uli Kunkel

Arne said:
Uli said:
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;


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top