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!