Author Topic: USB - PC memory leak in HID class.dll  (Read 7418 times)

bhushan

  • Member
  • ***
  • Posts: 2
USB - PC memory leak in HID class.dll
« 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;
                    }                
            }
        }
« Last Edit: March 14, 2014, 02:02:18 am by bhushan »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB - PC memory leak in HID class.dll
« Reply #1 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
« Last Edit: July 13, 2014, 10:06:00 pm by Jan Axelson »

bhushan

  • Member
  • ***
  • Posts: 2
Re: USB - PC memory leak in HID class.dll
« Reply #2 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
« Last Edit: July 13, 2014, 09:55:05 pm by Jan Axelson »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB - PC memory leak in HID class.dll
« Reply #3 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.