PORTS Forum

Ports and Interfaces => USB => Topic started by: bhushan on March 12, 2014, 10:56:29 am

Title: USB - PC memory leak in HID class.dll
Post by: bhushan on March 12, 2014, 10:56:29 am
I developed system using PIC18F26J50. I wrote a PC program using the Microchip “Generic HID - HID DLL Library” for communication . I am continuously reading the byte data using USBREADREPORT mehods. The program works for ~2 hours and then the performance degrades. I was looking with Windows Task Manager and observed that the Handle count increased continuously and increases in time. This indicates a memory leak. I thought that I have a mistake in my program but I insolated the problem and fond the memory leak in the HID class.dll.
Is there any solution that solved this problem?

Thanks in advance for any help.

 while (true)
             {
              lock (this)
                  {
                    unsafe
                        {
                            
                         if ((PerformUsBdataRead()) && (InputPacketBuffer[0] != Nodata))
                           {
                               //handle stylus events same as mouse events
                           }
                        }
                   }
               }
            


         //USB Read data
        private static bool PerformUsBdataRead()
        {
            unsafe
            {
                IntPtr unmanagedPointer = Marshal.AllocHGlobal(65);

                InputPacketBuffer[0] = 0;
                byte* inputPacketBufferPointer = (byte*)unmanagedPointer.ToPointer();
              
                    if (HIDClass.MCHPHIDClass.USBHIDReadReport(inputPacketBufferPointer))
                    {
                        unmanagedPointer = (IntPtr)inputPacketBufferPointer;
                        Marshal.Copy(unmanagedPointer, InputPacketBuffer, 0, 65);
                        Marshal.FreeHGlobal(unmanagedPointer);
                        return true;
                    }                  
                    else                
                    {
                        Marshal.FreeHGlobal(unmanagedPointer);
                        return false;
                    }                
            }
        }
Title: Re: USB - PC memory leak in HID class.dll
Post by: Jan Axelson on March 12, 2014, 11:02:57 am
If the problem is in a Microchip-provided DLL, try the Microchip web forum. I have HID example code here:

http://www.janaxelson.com/hidpage.htm#MyExampleCode
Title: Re: USB - PC memory leak in HID class.dll
Post by: bhushan on March 14, 2014, 02:14:01 am
If the problem is in a Microchip-provided DLL, try the Microchip web forum. I have HID example code here:

http://www.janaxelson.com/hidpage.htm#MyExampleCode

Thanks for your help.

I have one question. I checked USB connection in WM_DEVICECHANGE. After that When I read data without checking the connection, I didn't get any data. Why it requires to check the connection repeatedly?

Thanks in advance,
Bhushan Shelkar
Title: Re: USB - PC memory leak in HID class.dll
Post by: Jan Axelson on March 14, 2014, 05:22:44 pm
If you have a valid handle to the HID, you should be able to read data from it.