Author Topic: HidP_GetValueCaps causes corruption in C#?  (Read 12799 times)

vanweric

  • Member
  • ***
  • Posts: 24
HidP_GetValueCaps causes corruption in C#?
« on: November 16, 2011, 03:12:41 pm »
I am running into an issue that I'm having an unfortunately difficult time debugging.  My current best guess is that I am miscalling HidP_GetValueCaps and corrupting state in a way that the CLR does not pick up.

Symptom:
During HID device enumeration, the object representing the second device enumerated is corrupt.  One of the member variables shows up in the watch window as "Could Not Evaluate Expression".  Expanding the parent object shows lots of odd blank lines that look like extra member variables that are nameless and valueless.  Attempting to use the member variable throws a null reference exception, even though it just passed an "if(_variable != null)" check the line before.

if I explicitly set the member variable to null in the constructor, the code no longer crashes, but there are still phantom blank member variables in the watch window.

If I comment out HidP_GetValueCaps, the symptom goes away entirely.  The owning class contains no phantom members, and everything appears to work fine.


I took the declaration and usage of HidP_GetValueCaps directly from generic_hid_cs_50, and I am relatively confident that there is no transcription error.  My best guess is that it is passing an array of bytes of length vcSize, and it is expecting an array of HIDP_VALUE_CAPS instead.  Since the HIDP_VALUE_CAPS structure is quite big, I'm guessing it is blitting the values to adjacent memory and coincidentally hitting that member variable. 

To test this, I replaced Byte[] valueCaps = new Byte[vcSize]; with Byte[] valueCaps = new Byte[vcSize*1000];
It looks to have "fixed" it... but it doesn't make me feel too warm nor fuzzy.


Am I on the right track with this?
If so, why didn't the CLR catch the over-run?  Do I need to enable some extra sanity checking?  In otherwords, how do I know that I don't have similar errors lurking elsewhere?

Thanks!




Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HidP_GetValueCaps causes corruption in C#?
« Reply #1 on: November 16, 2011, 05:43:58 pm »
Just quickly, I haven't looked in detail, but maybe something here of use:

http://www.koders.com/csharp/fid70C0E277619B6B1A0C656BAEE4E1C3F6DC9DFAEB.aspx?s=usbs...

Jan
« Last Edit: November 16, 2011, 06:09:51 pm by Jan Axelson »

vanweric

  • Member
  • ***
  • Posts: 24
Re: HidP_GetValueCaps causes corruption in C#?
« Reply #2 on: November 23, 2011, 03:23:38 pm »
Jan -

Embarrassingly enough, it looks like I don't actually need to call that function in the first place.  Removing it looks to have 'fixed' the symptoms.

I believe that the example code I pulled it from will have a similar corruption bug, but unfortunately I am unable to devote time to looking in to it with deadlines looming.

 - Eric

ksrsrinivasan

  • Member
  • ***
  • Posts: 5
Re: HidP_GetValueCaps causes corruption in C#?
« Reply #3 on: December 10, 2013, 02:55:25 pm »
Hit the same issue. Posted the HidP_Value_Caps structure on http://www.janaxelson.com/forum/index.php?PHPSESSID=ac41e392e51b96aab2e15ed9de58ebb5&topic=713.0

The HidP_GetValueCaps import is
Code: [Select]
       [DllImport("hid.dll", SetLastError = true)]
        public static extern int HidP_GetValueCaps(HIDP_REPORT_TYPE reportType, [In, Out] HidP_Value_Caps[] valueCaps, ref UInt16 valueCapsLength, IntPtr preparsedData);
« Last Edit: July 25, 2014, 10:49:50 am by Jan Axelson »