Author Topic: USB HID Example Code  (Read 15818 times)

BobA

  • Member
  • ***
  • Posts: 3
USB HID Example Code
« on: March 22, 2011, 12:33:39 pm »
Greetings Jan... (and others!)
 Using the latest version of the USBHID (46) example code, I have encountered two problems.
One I fixed - in HID.CS \ internal override void Read - there are two calls to Marshal.AllocHGlobal which allocate buffers.
I noticed that my memory/handle use kept growing... and growing... and growing...
There are no calls to free this memory after use.  After adding those, memory stopped growing!  This is a good thing.

The thing I cannot explain is why in task manager/performance - the system handle count keeps rising and rising...
I have played around with this code, adding/removing calls to update the listboxes, and it seems to affect the handle count - I
imagine background GC is going on at varying ways.   Comments?

running win 7 32 bit , .net 4

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB HID Example Code
« Reply #1 on: March 22, 2011, 05:24:51 pm »
I'll add the Marshal.FreeHGlobal and will take a look at this, also a couple of other reported issues with this code.

Jan

BobA

  • Member
  • ***
  • Posts: 3
Re: USB HID Example Code
« Reply #2 on: March 23, 2011, 12:49:00 pm »
Jan,

I've been working on a little test program to spy this one out.

It basically captures handle count (GetProcessHandleCount) at various points along the way
from doing an interrupt write and an interrupt read.

The handle count does not change during the write part of this.
During a read, the count also does not change after the beginInvoke.

Upon entry to the read routine (GetInputReportData), the handle count has increased by 2.
at the end, after endInvoke, the count remains at +2.

Sometime later, the count gets decremented by 1, and sits at +1.

Each time through this sequence, the handle count gets incremented by one.

This is at least what I am seeing.

I am not a windows guy - I write firmware, so I am tapping with my trusty white cane...

BTW, I have a couple of your books - nice work!

BobA

  • Member
  • ***
  • Posts: 3
Re: USB HID Example Code
« Reply #3 on: March 23, 2011, 05:47:44 pm »
Talking to myself now.

Me thinks you need to closehandle on the eventobject created in hid.cs\prepare  ForOverlappedTransfer

After doing this, my handle count stopped growing!

Virgil Gomes

  • Member
  • ***
  • Posts: 15
Re: USB HID Example Code
« Reply #4 on: May 15, 2012, 12:51:33 pm »
Hi Jan and Bob,

I'm seeing the same memory and handle leak using the generic_CS_4.6. Can you please show me how this got resolved.

Thank you,
Virgil

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB HID Example Code
« Reply #5 on: May 15, 2012, 02:31:59 pm »
Try doing closehandle on the eventobject created in hid.cs\prepareForOverlappedTransfer as BobA suggested.

Jan

Virgil Gomes

  • Member
  • ***
  • Posts: 15
Re: USB HID Example Code
« Reply #6 on: May 15, 2012, 03:39:40 pm »
Hi Jan,


I tried closing handle in the hid.cs\prepareForOverlappedTransfer using the CancelTfransfer as shown below and got an exception:
      if (success)
            {
            // A report was received.
             // Copy the received data to inputReportBuffer for the application to use.

              Marshal.Copy(nonManagedBuffer, inputReportBuffer, 0, numberOfBytesRead);

                                               CancelTransfer(hidHandle, readHandle, writeHandle, eventObject);

                }
 Can you please explain to me how to do this.

Thank you,
Virgil


Virgil Gomes

  • Member
  • ***
  • Posts: 15
Re: USB HID Example Code
« Reply #8 on: May 16, 2012, 08:18:07 am »
Hi Jan,

Thank you. That was very helpfull.

Best regards,
Virgil

Virgil Gomes

  • Member
  • ***
  • Posts: 15
Re: USB HID Example Code
« Reply #9 on: May 16, 2012, 09:56:30 am »
Hi Jan,

Where in the code did you put  the "Marshal.FreeHGlobal"  to free up the memory leak?

Thank you,
Virgil


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB HID Example Code
« Reply #10 on: May 21, 2012, 11:57:41 am »
After a call to Marshal.AllocHGlobal, add Marshal.FreeHGlobal when finished using the buffer, usually at the end of the routine where the buffer is defined.

Jan