Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory Leak in JNI layer for unsigned char array
    primarykey
    data
    text
    <p>I have a JNI code that is used to make calls to .Net APIs from java.</p> <p>In one call, I convert a java byte array to a unsigned char array which is used for a .Net call.</p> <p>The unsigned char array that is created is wrapped over a Java Object and during the finalize call of that java object, i am clearing it.</p> <p>But on doing a continuous run, the memory just keeps on building which eventually ends up in OutOfMemoryException.</p> <h2>Here is the code that is creating the unsigned char array</h2> <pre><code>jbyte* messageBodyNativeJByteBuffer = env-&gt;GetByteArrayElements(jByteArrayValue,NULL); if(messageBodyNativeJByteBuffer == NULL){ RaiseError(DNJNI_ERROR_ALLOCATE, "unsigned char array"); } jsize arrayLength = env-&gt;GetArrayLength(jByteArrayValue); array&lt;unsigned char&gt;^ dataArray = gcnew array&lt;unsigned char&gt;(arrayLength); try{ for(int i=0;i&lt;arrayLength;i++){ dataArray[i] = messageBodyNativeJByteBuffer[i]; } return dataArray; }finally{ // Release the UTF8 bytes cout &lt;&lt; "Inside BuildByteArray finally block."; env-&gt;ReleaseByteArrayElements(jByteArrayValue, messageBodyNativeJByteBuffer,0); cout &lt;&lt; "Inside BuildByteArray finally block - After delete messageBodyNativeJByteBuffer."; delete messageBodyNativeJByteBuffer; env-&gt;DeleteLocalRef(jByteArrayValue); cout &lt;&lt; "Inside BuildByteArray finally block - After DeleteLocalRef jByteArrayValue."; } </code></pre> <hr> <p>and the clean up call, that clears it</p> <hr> <pre><code>Object^ object = DotNETJNIInterface::NativeState::findState(identifier); //This if is I added recently to try deleting it, but does not help if(object-&gt;GetType()-&gt;ToString()-&gt;Equals("System.Byte[]")){ array&lt;unsigned char&gt;^ toBeDeleted = dynamic_cast&lt;array&lt;unsigned char&gt;^&gt;(object); delete [] toBeDeleted; cout &lt;&lt; "Deleted Byte Array by self::::"; } DotNETJNIInterface::NativeState::removeState(identifier); </code></pre> <hr> <p>Please let me know if there is something I am missing that is causing the byte array memory not to be deleted.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload