PORTS Forum
Ports and Interfaces => USB => Topic started 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
-
I'll add the Marshal.FreeHGlobal and will take a look at this, also a couple of other reported issues with this code.
Jan
-
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!
-
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!
-
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
-
Try doing closehandle on the eventobject created in hid.cs\prepareForOverlappedTransfer as BobA suggested.
Jan
-
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
-
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
-
Hi Jan,
Thank you. That was very helpfull.
Best regards,
Virgil
-
Hi Jan,
Where in the code did you put the "Marshal.FreeHGlobal" to free up the memory leak?
Thank you,
Virgil
-
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