Revert "Don't leak device handle in macOS 10.10 or newer"

This reverts commit 5925c27efb.

Fixes https://github.com/libsdl-org/SDL/issues/12807

(cherry picked from commit 5b951141d2)
This commit is contained in:
Sam Lantinga
2025-04-29 09:48:44 -07:00
parent 24ccde693e
commit 7fc5edab8e
3 changed files with 6 additions and 19 deletions

View File

@@ -79,7 +79,7 @@ case $host in
backend="mac" backend="mac"
os="darwin" os="darwin"
threads="pthreads" threads="pthreads"
LIBS="${LIBS} -framework IOKit -framework CoreFoundation -framework AppKit" LIBS="${LIBS} -framework IOKit -framework CoreFoundation"
;; ;;
*-freebsd*) *-freebsd*)
AC_MSG_RESULT([ (FreeBSD back-end)]) AC_MSG_RESULT([ (FreeBSD back-end)])

View File

@@ -14,7 +14,7 @@ COBJS=hid.o
CPPOBJS=../hidtest/hidtest.o CPPOBJS=../hidtest/hidtest.o
OBJS=$(COBJS) $(CPPOBJS) OBJS=$(COBJS) $(CPPOBJS)
CFLAGS+=-I../hidapi -Wall -g -c CFLAGS+=-I../hidapi -Wall -g -c
LIBS=-framework IOKit -framework CoreFoundation -framework AppKit LIBS=-framework IOKit -framework CoreFoundation
hidtest: $(OBJS) hidtest: $(OBJS)

View File

@@ -37,9 +37,6 @@
#define VALVE_USB_VID 0x28DE #define VALVE_USB_VID 0x28DE
/* As defined in AppKit.h, but we don't need the entire AppKit for a single constant. */
extern const double NSAppKitVersionNumber;
/* Barrier implementation because Mac OSX doesn't have pthread_barrier. /* Barrier implementation because Mac OSX doesn't have pthread_barrier.
It also doesn't have clock_gettime(). So much for POSIX and SUSv2. It also doesn't have clock_gettime(). So much for POSIX and SUSv2.
This implementation came from Brent Priddy and was posted on This implementation came from Brent Priddy and was posted on
@@ -134,7 +131,6 @@ struct hid_device_list_node
}; };
static IOHIDManagerRef hid_mgr = 0x0; static IOHIDManagerRef hid_mgr = 0x0;
static int is_macos_10_10_or_greater = 0;
static struct hid_device_list_node *device_list = 0x0; static struct hid_device_list_node *device_list = 0x0;
static hid_device *new_hid_device(void) static hid_device *new_hid_device(void)
@@ -489,7 +485,6 @@ static int init_hid_manager(void)
int HID_API_EXPORT hid_init(void) int HID_API_EXPORT hid_init(void)
{ {
if (!hid_mgr) { if (!hid_mgr) {
is_macos_10_10_or_greater = (NSAppKitVersionNumber >= 1343); /* NSAppKitVersionNumber10_10 */
return init_hid_manager(); return init_hid_manager();
} }
@@ -1143,10 +1138,8 @@ void HID_API_EXPORT hid_close(hid_device *dev)
if (!dev) if (!dev)
return; return;
/* Disconnect the report callback before close. /* Disconnect the report callback before close. */
See comment below. if (!dev->disconnected) {
*/
if (is_macos_10_10_or_greater || !dev->disconnected) {
IOHIDDeviceRegisterInputReportCallback( IOHIDDeviceRegisterInputReportCallback(
dev->device_handle, dev->input_report_buf, dev->max_input_report_len, dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
NULL, dev); NULL, dev);
@@ -1169,14 +1162,8 @@ void HID_API_EXPORT hid_close(hid_device *dev)
/* Close the OS handle to the device, but only if it's not /* Close the OS handle to the device, but only if it's not
been unplugged. If it's been unplugged, then calling been unplugged. If it's been unplugged, then calling
IOHIDDeviceClose() will crash. IOHIDDeviceClose() will crash. */
if (!dev->disconnected) {
UPD: The crash part was true in/until some version of macOS.
Starting with macOS 10.15, there is an opposite effect in some environments:
crash happenes if IOHIDDeviceClose() is not called.
Not leaking a resource in all tested environments.
*/
if (is_macos_10_10_or_greater || !dev->disconnected) {
IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeNone); IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeNone);
} }