PORTS Forum

Ports and Interfaces => USB => Topic started by: BobA on March 22, 2011, 12:33:39 pm

Title: USB HID Example Code
Post by: BobA 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
Title: Re: USB HID Example Code
Post by: Jan Axelson 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
Title: Re: USB HID Example Code
Post by: BobA 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!
Title: Re: USB HID Example Code
Post by: BobA 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!
Title: Re: USB HID Example Code
Post by: Virgil Gomes 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
Title: Re: USB HID Example Code
Post by: Jan Axelson on May 15, 2012, 02:31:59 pm
Try doing closehandle on the eventobject created in hid.cs\prepareForOverlappedTransfer as BobA suggested.

Jan
Title: Re: USB HID Example Code
Post by: Virgil Gomes 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
Title: Re: USB HID Example Code
Post by: Jan Axelson on May 15, 2012, 04:55:18 pm
I don't have tested example code but these might help:

http://www.pinvoke.net/default.aspx/kernel32.closehandle

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915%28v=vs.85%29.aspx

Jan
Title: Re: USB HID Example Code
Post by: Virgil Gomes on May 16, 2012, 08:18:07 am
Hi Jan,

Thank you. That was very helpfull.

Best regards,
Virgil
Title: Re: USB HID Example Code
Post by: Virgil Gomes 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

Title: Re: USB HID Example Code
Post by: Jan Axelson 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