Open VerusMaya opened 3 years ago
Following up since it has been a bit.
Have you had a chance to look into this?
It would be great to get an updated nuget package with the memory leak resolved.
Thank you!
?
Great work finding this. 💯 forked and fixed. until this gets merged into main branch.
Describe the issue I observed an ongoing unmanaged memory leak when regularly polling for connected devices. I reproduced it with a minimal CLI .exe and narrowed it down to
WindowsHidApiService.GetHidCapabilities
. To resolve the leak, change theHidD_FreePreparsedData
pointerToPreparsedData
parameter to not beref
.The
HidD_GetPreparsedData
documentation shows a*PreparsedData
parameter. This matches with theout
parameter option used. https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/hidsdi/nf-hidsdi-hidd_getpreparseddataThe
HidD_FreePreparsedData
documentation shows aPreparsedData
parameter with no dereference. I believe it should therefore be called withoutref
. https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/hidsdi/nf-hidsdi-hidd_freepreparseddataRelevant call stack -
WindowsHidApiService.GetHidCapabilities
WindowsHidApiService.GetDeviceDefinition
WindowsHidDeviceFactoryExtensions.GetDeviceDefinition
WindowsDeviceEnumerator.GetConnectedDeviceDefinitionsAsync
Screenshots Memory profiler running for one minute. Notice the bottom-right section for unmanaged memory breakdown by module.
without fix
with fix
Your Code The memory leak can be reproduced easily.
The leak can be resolved with two changes. https://github.com/MelbourneDeveloper/Device.Net/blob/417330ef418a7bacce36dae29eb6f67fc7620723/src/Hid.Net/Windows/WindowsHidApiService.cs#L61
https://github.com/MelbourneDeveloper/Device.Net/blob/417330ef418a7bacce36dae29eb6f67fc7620723/src/Hid.Net/Windows/WindowsHidApiService.cs#L117
Log / Stack Trace No relevant logging or stack trace.
Info
PR coming soon.