Menu

Bulk Write Memory Leak

Help
Blaine
2008-12-18
2012-12-06
  • Blaine

    Blaine - 2008-12-18

    I am using the LibusbJava.usb_bulk_write method on Windows.  I have claimed the interface previously and I have a valid handle.  Everytime I call it with a byte array of 2112 bytes the memory is never recovered.  I can watch the available physical memory in my task manager continually drop in about 1.6Mbyte chunks.  If I comment out the one line of code in my app that calls this bulk_write method, the memory leak goes away.  When I try and profile the app in Netbeans I don't see the memory leak so I believe it is possibly at the driver level.  I am calling the bulk_write method 2 million times in a tight loop.  I looked at the source code for the driver and I don't see anything out of the ordinary.  This is a show stopper and preventing me from devlivering my product on time.  Any help would be appreciated.  Here is a snippet of my code:

    public synchronized void bulkWrite(byte[] data) {
       if(handle != 0) {
          int bytesWritten = LibusbJava.usb_bulk_write(handle, 4, data, data.length, 500);
       }
    }

    thx in advance.

     
    • Blaine

      Blaine - 2008-12-19

      I have downloaded the source code, modified it, and verified that there is a memory leak in the following 3 functions:

      Java_ch_ntb_usb_LibusbJava_usb_1control_1msg
      Java_ch_ntb_usb_LibusbJava_usb_1bulk_1write
      Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1write

      The problem is that env->GetByteArrayElements is called but never followed up with a env->ReleaseByteArrayElements.  When I call the bulk_write function over a million times I can watch the physical memory of my system become depleted and eventually my program crashes.  When I add the ReleaseByteArrayElements the problem goes away.

      Here is the code after my changes to just the bulk_write function as an example:

      JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1write
        (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout)
        {
            clearLibusbJavaError();
            jbyte *bytes = env->GetByteArrayElements(jbytes, NULL);
          jint num_bytes = usb_interrupt_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout);
          env->ReleaseByteArrayElements(jbytes, bytes, 0);
           return num_bytes;
        }

       
      • andi

        andi - 2008-12-20

        fixed in the newest release

         
        • Blaine

          Blaine - 2009-01-05

          You're welcome.

           
          • andi

            andi - 2009-01-05

            I guess you don't read the e-mails sent to your e-mail address which is registered for bbergeson8?

            18.12.2008 22:43 -> I wrote that I have reproduced the problem and sent you a corrected DLL

            But anyway: THANK YOU VERY MUCH that you have pointed out the problem!

             
    • Blaine

      Blaine - 2009-01-05

      You're welcome.

       
    • Blaine

      Blaine - 2009-01-06

      I have searched all my email and I can't find any email that contained the information you suggested so I am not sure what happened because I did receive the bug report email that I submitted and I have received the emails that contained the responses to this thread.

      Anyway, thanks for the comment.  I am glad I was able to help.  It feels good to be able to give back once in awhile.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.