google / gousb

gousb provides low-level interface for accessing USB devices
Apache License 2.0
845 stars 124 forks source link

Dereference device with a defer call to prevent memory leaks #98

Closed danielpaulus closed 3 years ago

danielpaulus commented 3 years ago

This PR fixes the memory leak mentioned in #97

gousb.OpenDevices currently only dereferences libusb device pointers when opening is unsuccessful or when the opener returns false. This can cause a memory leak that becomes mostly visible when using multiple libusb contexts in a long running application.

The fix is to always call c.libusb.dereference(dev) so the device reference counter is decreased when OpenDevices is done. That is not a problem as libusb increases the reference counter internally on the c.libusb.open(dev) call and releases it when we call c.libusb.close(dev) again.

google-cla[bot] commented 3 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

coveralls commented 3 years ago

Coverage Status

Coverage increased (+0.1%) to 79.409% when pulling e40a20173bcdf105403b0f7607b468652154165a on danielpaulus:fix-memleak-in-opendevices into 0eba1b126450dc6c62ded3542c825f645789a226 on google:master.

coveralls commented 3 years ago

Coverage Status

Coverage increased (+0.2%) to 79.431% when pulling 9cc76590a7709cb33cf451d1917ff19864cfe2d3 on danielpaulus:fix-memleak-in-opendevices into 0eba1b126450dc6c62ded3542c825f645789a226 on google:master.

danielpaulus commented 3 years ago

@googlebot I signed it!

danielpaulus commented 3 years ago

Coverage Status

Coverage increased (+0.1%) to 79.409% when pulling e40a201 on danielpaulus:fix-memleak-in-opendevices into 0eba1b1 on google:master.

wow I even increased code coverage ;-)

danielpaulus commented 3 years ago

could you also update a comment in https://github.com/google/gousb/blob/master/libusb.go#L212? it is misleading now as it is, since it implies that all devices returned on the list will be "closed" (and so also opened) at some point, but this is not the case.

Done. Should I also create a small unit test for OpenDevices while I am at it?

danielpaulus commented 3 years ago

hey @zagrodzki , is there anything missing on the PR? If not, when do you think you can release it? I would like to move away from using my private fork :-)

zagrodzki commented 3 years ago

sorry, I assumed you will be able to merge yourself after my approval, but it seems this is not how GitHub flow works...

There is one comment left: typo in libusb.go (https://github.com/google/gousb/pull/98#discussion_r621199462)