Commit 245350a
committed
core: release device refs in destructor to fix exit-time leaks (#25)
NOTE: This patch was generated by an AI coding assistant (Claude). A
human contributor should review the diagnosis and the change before
merging. See the Assisted-by trailer below, per the format suggested
in https://docs.kernel.org/process/coding-assistants.html.
The destructor only called libusb_exit(ctx); it never walked usb_busses
to free the usb_device wrappers populated by usb_find_devices(). Each
wrapper holds a libusb_ref_device() taken in initialize_device(), so at
process exit those refs are still outstanding and libusb_exit prints
"device X.Y still referenced" warnings.
Free the bus/device tree in _usb_exit before libusb_exit so each
libusb_unref_device() in free_device() runs. The same helper also fixes
the bus-removed branch in usb_find_busses, which previously freed the
bus struct without freeing its devices.
The ref/unref in initialize_device/free_device is intentionally kept:
libusb_free_device_list(dev_list, 1) at the end of usb_find_devices
drops the temporary refs, so without the extra ref taken here dev->dev
would dangle for any device retained in bus->devices, causing
use-after-free in usb_open() and elsewhere (this is what PR #30 missed).
Assisted-by: Claude:claude-opus-4-7
https://claude.ai/code/session_01JdNX3ZQuo2ysAVSRJtxNPo1 parent f16e0b3 commit 245350a
1 file changed
Lines changed: 22 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
84 | 97 | | |
85 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
86 | 107 | | |
87 | 108 | | |
88 | 109 | | |
| |||
317 | 338 | | |
318 | 339 | | |
319 | 340 | | |
320 | | - | |
| 341 | + | |
321 | 342 | | |
322 | 343 | | |
323 | 344 | | |
| |||
0 commit comments