Ports
May 18, 2013, 04:10:27 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Welcome to the PORTS Forum. Please post your questions, answers, and comments relating to USB and other hardware interfaces in the appropriate forums below. - Jan
Pages: [1]
  Print  
Author Topic: USB HID Example Code  (Read 3279 times)
BobA
Member
*
Posts: 3


« on: March 22, 2011, 11:33:39 am »

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
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1624


WWW
« Reply #1 on: March 22, 2011, 04: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
Logged
BobA
Member
*
Posts: 3


« Reply #2 on: March 23, 2011, 11:49:00 am »

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!
Logged
BobA
Member
*
Posts: 3


« Reply #3 on: March 23, 2011, 04: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!
Logged
Virgil Gomes
Member
***
Posts: 15


« Reply #4 on: May 15, 2012, 11:51:33 am »

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
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1624


WWW
« Reply #5 on: May 15, 2012, 01:31:59 pm »

Try doing closehandle on the eventobject created in hid.cs\prepareForOverlappedTransfer as BobA suggested.

Jan
Logged
Virgil Gomes
Member
***
Posts: 15


« Reply #6 on: May 15, 2012, 02: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
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1624


WWW
« Reply #7 on: May 15, 2012, 03: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
Logged
Virgil Gomes
Member
***
Posts: 15


« Reply #8 on: May 16, 2012, 07:18:07 am »

Hi Jan,

Thank you. That was very helpfull.

Best regards,
Virgil
Logged
Virgil Gomes
Member
***
Posts: 15


« Reply #9 on: May 16, 2012, 08:56:30 am »

Hi Jan,

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

Thank you,
Virgil

Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1624


WWW
« Reply #10 on: May 21, 2012, 10: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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!