Skip to content

Commit

Permalink
Automatically self.close() for safe HIDAPI finalization
Browse files Browse the repository at this point in the history
hid_exit() is only safe to call once every hid_device has been closed.

Fixes: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
  • Loading branch information
jonasmalacofilho authored and prusnak committed Nov 2, 2021
1 parent 8f01094 commit 987dd3c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@ cdef class device:
A device instance can be used to read from and write to a HID device.
"""

cdef hid_device *_c_hid
cdef object __weakref__ # enable weak-reference support

def __cinit__(self):
"""Initialize the device instance.
Sets up `self.close()` to be automatically called once the strong count
goes to zero. This is necessary to prevent issues later in the
execution, when finalizing the HIDAPI library itself.
"""
weakref.finalize(self, self.close)

def open(self, int vendor_id=0, int product_id=0, unicode serial_number=None):
"""Open the connection.
Expand Down

0 comments on commit 987dd3c

Please sign in to comment.