Skip to content

Commit

Permalink
Finalize HIDAPI only when ref count reaches zero
Browse files Browse the repository at this point in the history
Replace atexit.register() with weakref.finalize(), since that should
guarantee that there are no remaining references to the module (and,
thus, HIDAPI) when it runs.

Related: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
  • Loading branch information
jonasmalacofilho authored and prusnak committed Nov 2, 2021
1 parent 4e9f45b commit 8f01094
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions hid.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import atexit
import weakref

from chid cimport *
from libc.stddef cimport wchar_t, size_t

Expand Down Expand Up @@ -382,5 +383,6 @@ cdef class device:
return U(<wchar_t*>hid_error(self._c_hid))


# Set a callback to close the HID library as the script exits
atexit.register(hidapi_exit)
# Finalize the HIDAPI library *only* once there are no more references to this
# module, and it is being garbage collected.
weakref.finalize(sys.modules[__name__], hidapi_exit)

0 comments on commit 8f01094

Please sign in to comment.