I'm writing a Mac application that interfaces with a USB HID device. To get started I'm trying to print out how many devices were found that match the specifications of my device.
//Create a HID Manager
IOHIDManagerRef hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
//Create a dictionary and limit it to the uPPT
CFMutableDictionaryRef dict = CFDictionaryCreateMutable (kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(dict, CFSTR("VendorID"), CFSTR("0x04D8"));
CFDictionarySetValue(dict, CFSTR("ProductID"), CFSTR("0x0054"));
IOHIDManagerSetDeviceMatching(hidManager, dict);
CFSetRef devSet = IOHIDManagerCopyDevices (hidManager);
CFIndex numDevices = CFSetGetCount(devSet);
NSLog(@"%ld uPPTs found", numDevices);
Unfortunately this crashes at CFSetGetCount(devSet). As far as I can tell I'm using this correctly, but the application hangs and I see "Thread 1: EXC_BAD_ACCESS (Code=1, Address=0x0)". Any ideas? Thanks
devSet
is NULL
(as evidenced in your crash by Address=0x0
.) You must always test for NULL
when getting or creating a CFTypeRef
in Core Foundation because Apple's APIs don't do it and will usually crash if passed NULL
.
As well, this isn't the cause of the crash, but you must make sure to release (with CFRelease()
) CFTypeRef
s you create, copy, or retain, as ARC does not do it for you. And as previously stated, you must check for NULL
before calling CFRelease()
because a crash will happen otherwise.