From a862fdaa938444faa3e3f4ef187d4a0c6a40d1b8 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Fri, 25 Sep 2020 10:23:20 +0200 Subject: [PATCH] Make sure to release objects in on Mac. --- fido2/hid/macos.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/fido2/hid/macos.py b/fido2/hid/macos.py index 52c755e..f07d698 100644 --- a/fido2/hid/macos.py +++ b/fido2/hid/macos.py @@ -386,28 +386,28 @@ def list_descriptors(): hid_mgr = iokit.IOHIDManagerCreate(None, 0) if not hid_mgr: raise OSError("Unable to obtain HID manager reference") - iokit.IOHIDManagerSetDeviceMatching(hid_mgr, None) + try: + iokit.IOHIDManagerSetDeviceMatching(hid_mgr, None) - # Get devices from HID manager - device_set_ref = iokit.IOHIDManagerCopyDevices(hid_mgr) - if not device_set_ref: - raise OSError("Failed to obtain devices from HID manager") - - num = iokit.CFSetGetCount(device_set_ref) - devices = (IO_HID_DEVICE_REF * num)() - iokit.CFSetGetValues(device_set_ref, devices) - - # Retrieve and build descriptor dictionaries for each device - descriptors = [] - for handle in devices: + # Get devices from HID manager + device_set_ref = iokit.IOHIDManagerCopyDevices(hid_mgr) + if not device_set_ref: + raise OSError("Failed to obtain devices from HID manager") try: - descriptors.append(_get_descriptor_from_handle(handle)) - except Exception as e: - logger.debug("Failed opening HID device", exc_info=e) - continue + num = iokit.CFSetGetCount(device_set_ref) + devices = (IO_HID_DEVICE_REF * num)() + iokit.CFSetGetValues(device_set_ref, devices) - # Clean up CF objects - cf.CFRelease(device_set_ref) - cf.CFRelease(hid_mgr) - - return descriptors + # Retrieve and build descriptor dictionaries for each device + descriptors = [] + for handle in devices: + try: + descriptors.append(_get_descriptor_from_handle(handle)) + except Exception as e: + logger.debug("Failed opening HID device", exc_info=e) + continue + return descriptors + finally: + cf.CFRelease(device_set_ref) + finally: + cf.CFRelease(hid_mgr)