Darwin: Fix #63 error when apps use Objective-C garbage collection
authorSean McBride <sean@rogue-research.com>
Thu, 10 Feb 2011 22:11:29 +0000 (17:11 -0500)
committerPeter Stuge <peter@stuge.se>
Mon, 13 Jun 2011 20:06:32 +0000 (22:06 +0200)
configure.ac
libusb/os/darwin_usb.c

index bf2875e..e21e4dd 100644 (file)
@@ -59,6 +59,7 @@ case $host in
        AC_MSG_RESULT([Darwin/Mac OS X])
        backend="darwin"
        threads="posix"
+       LIBS="${LIBS} -lobjc"
        PC_LIBS_PRIVATE="-Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
        LTLDFLAGS="${LTLDFLAGS} -Wl,-prebind"
        AC_CHECK_HEADERS([poll.h])
index 7927b3a..912b65c 100644 (file)
 #include <mach/clock.h>
 #include <mach/clock_types.h>
 #include <mach/mach_host.h>
-
 #include <mach/mach_port.h>
+
+#include <AvailabilityMacros.h>
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
+  #include <objc/objc-auto.h>
+#endif
+
 #include <IOKit/IOCFBundle.h>
 #include <IOKit/usb/IOUSBLib.h>
 #include <IOKit/IOCFPlugIn.h>
@@ -264,6 +269,14 @@ static void *event_thread_main (void *arg0) {
   struct libusb_context *ctx = (struct libusb_context *)arg0;
   CFRunLoopRef runloop;
 
+  /* Tell the Objective-C garbage collector about this thread.
+     This is required because, unlike NSThreads, pthreads are
+     not automatically registered. Although we don't use
+     Objective-C, we use CoreFoundation, which does. */
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
+  objc_registerThreadWithCollector();
+#endif
+
   /* hotplug (device removal) source */
   CFRunLoopSourceRef     libusb_notification_cfsource;
   io_notification_port_t libusb_notification_port;