HidLibrary icon indicating copy to clipboard operation
HidLibrary copied to clipboard

HidDevices.EnumerateHidDevices memory leak due to yield

Open jj-jabb opened this issue 13 years ago • 7 comments

Running on both XP (32bit) and Windows 7 (64bit) with the Logitech G13 controller - I've noticed severe memory leaking and it appears to be caused by using the yield keyword at line 69, preventing NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet) at line 72 from ever being reached. This can be fixed by removing the yield statement, moving var devices = new List() at line 51 to before the if statement, and returning devices.

Thanks for the great work!

jj-jabb avatar Apr 07 '11 21:04 jj-jabb

im not sure thats the right way to fix... this to me is the right way... line 69 onward: //yield return devicePath; } } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);

foreach (string devicePath in devices) { yield return devicePath; }

                            }

mitchjs avatar Apr 23 '11 19:04 mitchjs

so that it builds the list of devicepaths... then yield a devicepath for each literation though the IEnumerable and this allows you to stop your illertation at any point, and not have deviceInfoSet left in memory

mitchjs avatar Apr 23 '11 19:04 mitchjs

devices is already an IEnumerable - might as well just return it instead of yielding each string.

jj-jabb avatar Apr 26 '11 04:04 jj-jabb

true, i guess, but then we cant use the cool YIELD statement :)

mitchjs avatar Apr 26 '11 13:04 mitchjs

Thank you so much, this cured my memory leak!

jrockfl avatar Oct 16 '11 00:10 jrockfl

Worked for me also. Thanks!

jw834ksj43 avatar Oct 20 '11 21:10 jw834ksj43

Can someone make a pull request for this and possibly unit tests?

ghost avatar Nov 04 '15 03:11 ghost